Select last insert id

匿名 (未验证) 提交于 2019-12-03 02:46:02

问题:

I am inserting a record and i want to use the id of the last record inserted. This is what i have tried:

    $sql =      'INSERT INTO customer (first_name, last_name, email, password, date_created, dob, gender, customer_type) VALUES(:first_name, :last_name, :email,  :password,  :date_created,  :dob,  :gender,  :customer_type)' . ' SELECT LAST_INSERT_ID()' ; 

I am getting the error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT LAST_INSERT_ID()'. Can anyone show me where is my mistake? Thanks!

回答1:

Check out mysql_insert_id()

mysql_query($sql); $id = mysql_insert_id(); 

When that function is run after you've executed your INSERT statement in a mysql_query() command its result will be the ID of the row that was just created.



回答2:

You can do another query:

$last_id = "SELECT LAST_INSERT_ID()";

Or try to add ; in your query:

INSERT INTO customer (first_name, last_name, email, password, date_created, dob, gender, customer_type) VALUES(:first_name, :last_name, :email, :password, :date_created, :dob, :gender, :customer_type);' . ' SELECT LAST_INSERT_ID()';



文章来源: Select last insert id
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!