tags:

views:

214

answers:

1

Hello! 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!

+7  A: 

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 it's result will be the ID of the row that was just created.

Cryo
Thanks a lot!It worked! :)
chupinette
@chupinette Glad to help.
Cryo
Actually im having a problem now, mysql_insert_id() [function.mysql-insert-id]: Access denied for user 'ODBC'@'localhost'
chupinette