简体   繁体   中英

Select last insert id

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!

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.

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)<b>;</b>' . ' SELECT LAST_INSERT_ID()';

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