get id of last inserted record without using mysql_insert_id()

风流意气都作罢 提交于 2019-12-11 15:42:45

问题


$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'


回答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.




回答2:


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




回答3:


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.




回答4:


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?



来源:https://stackoverflow.com/questions/2271230/get-id-of-last-inserted-record-without-using-mysql-insert-id

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