开发者

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

开发者 https://www.devze.com 2023-01-24 21:29 出处:网络
I need to insert s开发者_StackOverflow中文版ome 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.

I need to insert s开发者_StackOverflow中文版ome 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消