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

Can anyone tell me if the syntax is right? I am having problems using mysql_insert_id that is why i am using SELECT LAST_INSERT_ID()

the error message: mysql_insert_id() [function.mysql-insert-id]: Access denied for user 'ODBC'@'localhost'

chupinette
  • 456
  • 3
  • 9
  • 22

4 Answers4

1

The SELECT doesn't make sense in the context of the query. Execute a separate query for it.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

No, it makes no sense whatever. If you really wanted to do select last_insert_id, you should do it in a separate (indeed, typically the next) statement.

But there's no need to do that as you can do it at the API level. Just call your appropriate function for your API to get the last insert ID instead. There's no need for a separate statement.

MarkR
  • 62,604
  • 14
  • 116
  • 151
1

Firstly, you need to use a semi-colon to separate two queries, secondly mysql_query won't allow you to execute two queries simultaneously, thirdly the "SELECT LAST_INSERT_ID" might become problematic if you have many concurrent users (collisions may happen). What is your problem with the mysql_insert_id?

KCL
  • 6,733
  • 10
  • 37
  • 43
  • 3
    LAST_INSERT_ID() is per connection, so there is no problem with concurrent usage as each PHP page will get its own connection to the DB. – Matthew Feb 16 '10 at 07:35
1

It will work if you add the closing ; between the two queries (at least MySQL will accept it, I don't know if PHP will complain). But, as above, it's not very good code and, if you separate it, will be a race condition. Figure out your kinks with mysql_insert_id() and use that like it's designed for.

Brock Batsell
  • 5,786
  • 1
  • 25
  • 27