简体   繁体   中英

CodeIgniter - Getting id from a freshly inserted database record in the same function

I need to insert some details into a database and get the auto_incremented ID of the record that has just been inserted into the database in the same function.

Firstly i use the controller to tell the model to insert all the data using the following:

$this->client->add_new_client();

Then straight after that i need to insert the clients ID, which was automatically generated by the database when the details where added to the database, to another model using:

$this->model->add_new_details($client_id);

Any help would be fantastic.

Cheers

You want to go ahead and return $this->db->insert_id(); from the first function.

If you have some way to get at the client object, you can probably call its method that returns an ID and get the ID. I'm not sure enough about CI to tell you how.

In PHP generally, call mysql_insert_id() to get the last ID generated on the current database connection. It should return the ID of the client if that's the last INSERT query that ran.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM