Codeigniter Activerecord, Retrieve Last Insert Id

how to get last insert id after insert query in codeigniter active record

Try this

function add_post($post_data){
$this->db->insert('posts', $post_data);
$insert_id = $this->db->insert_id();

return $insert_id;
}

In case of multiple inserts you could use

$this->db->trans_start();
$this->db->trans_complete();

CodeIgniter activerecord, retrieve last insert id?

Shame on me...

I looked at the user guide and the first function is $this->db->insert_id();

This also works with activerecord inserts...

EDIT: I updated the link

how to get last insert id after insert query in codeigniter

You have to assign value in variable after assigning you will able to use it.

switch ($subject) {
case 1:
$this->load->model('insertquestion_model');
$id = $this->insertquestion_model->insert_cropsci($question_data);
echo $id;
break;

How to get insert id after save to database in CodeIgniter 4

I got a simple solution after researching on the core of the CI 4 framework.

$db = db_connect('default'); 
$builder = $db->table('myTable');

$data = [
'username' => 'darth',
'email' => 'd.vader@theempire.com'
];

$builder->insert($data);
echo $db->insertID();

Hope they'll add a clear description on the docs soon.

i can't get last insert id from database in codeigniter

Put the code for getting last insert ID after inserting the record in the DB.

$this->db->insert($this->tbl, $data);
$custom_id = $this->custIdPetugas();

But if you want to get before insert the record use this.
Suppose your last insert ID is 99 it will give you 100 in return

SELECT AUTO_INCREMENT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'database_name'
AND TABLE_NAME = 'table_name';

Another way to get last insert ID before inserting the record.

$last_id = SELECT MAX(id) FROM table;

Increment by value 1 for next record

Model

public function custIdPetugas() {

$prefix = "PTGS" . "-";
//Suppose last ID is 23

$lastid = $this->db->query('SELECT MAX(id) as max_id FROM table')->row();

//$lastid = 23; Increment it by one for next unique value
$customid = $prefix.$lastid->max_id;

return $customid;

}

At insert_id() , get last insert id in codeigniter extra 1 is coming with id

Hope this will help you :

Note : Use only echo to print the insert id in your model; and to get the id in controller just return $insert_id

Your model should be like this :

public function create($data = [])
{
if (! empty($data))
{
$this->db->insert($this->table,$data);
$insert_id = $this->db->insert_id();
return $insert_id;
}
}

In your controller :

Note : make sure you have loaded your model either in autoload or in controller

public function method_name()
{
$data = ['your insert data in array'];
$id = $this->model_name->create($data);
echo $id;//you will get the id;
}

How to get a value from the last inserted row using Codeigniter

try this code:

$this->db->insert_id();

Refer:
https://www.codeigniter.com/user_guide/database/helpers.html

Codeigniter function to retrieve last inserted mysql value

You can also get last record by using

$id = $this->db->insert_id();
$query = $this->db->get_where('your_table_name', array('your_id_field' => $id));
return $query->row();


Related Topics



Leave a reply



Submit