Select last insert id

I am inserting a record and I want to use the identifier of the last inserted record. This is what I 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 get an error:
You have an error in the SQL syntax; check the manual that matches your MySQL server version for the correct syntax to use next to "SELECT LAST_INSERT_ID ()". Can someone show me where my mistake is? Thanks!

+5
source share
2 answers

Check out mysql_insert_id ()

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

When this function is run after you execute the INSERT statement in the mysql_query () command, its result will be the identifier of the row just created.

+12

:

$last_id = "SELECT LAST_INSERT_ID()";

; :

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()';

0

All Articles