radiations3 Posted August 7, 2011 Share Posted August 7, 2011 I am using the following code to return the last insert id from the table employee kindly let me know what is wrong with the following code: <?php $temp_array = mysql_query("select last_insert_id() from employee"); //now you can display it, to test it echo $temp_array; ?> Quote Link to comment https://forums.phpfreaks.com/topic/244127-problem-in-returning-the-last-insert-id/ Share on other sites More sharing options...
radiations3 Posted August 7, 2011 Author Share Posted August 7, 2011 its not returning any value.... Quote Link to comment https://forums.phpfreaks.com/topic/244127-problem-in-returning-the-last-insert-id/#findComment-1253719 Share on other sites More sharing options...
AbraCadaver Posted August 7, 2011 Share Posted August 7, 2011 its not returning any value.... mysql_fetch_array() ? Quote Link to comment https://forums.phpfreaks.com/topic/244127-problem-in-returning-the-last-insert-id/#findComment-1253723 Share on other sites More sharing options...
radiations3 Posted August 7, 2011 Author Share Posted August 7, 2011 On the following code <?php $temp_array = mysql_fetch_array(mysql_query("select last_insert_id() from employee")); $my_last_id = $temp_array['last_insert_id()']; //now you can display it, to test it echo $my_last_id ; ?> following error is occuring: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in D:\DBMS\untitled.php on line 10 Quote Link to comment https://forums.phpfreaks.com/topic/244127-problem-in-returning-the-last-insert-id/#findComment-1253725 Share on other sites More sharing options...
Pikachu2000 Posted August 7, 2011 Share Posted August 7, 2011 PHP has a built in function for that: mysql_insert_id. If you need to use the MySQL LAST_INSERT_ID() function directly, read the manual entry for it. Your syntax is off. Quote Link to comment https://forums.phpfreaks.com/topic/244127-problem-in-returning-the-last-insert-id/#findComment-1253730 Share on other sites More sharing options...
radiations3 Posted August 7, 2011 Author Share Posted August 7, 2011 How to use mysql_insert_id() to retrieve the last id from the table employee KINDLY HELP!!!??? Quote Link to comment https://forums.phpfreaks.com/topic/244127-problem-in-returning-the-last-insert-id/#findComment-1253750 Share on other sites More sharing options...
radiations3 Posted August 7, 2011 Author Share Posted August 7, 2011 HELP ANY1?? Quote Link to comment https://forums.phpfreaks.com/topic/244127-problem-in-returning-the-last-insert-id/#findComment-1253757 Share on other sites More sharing options...
radiations3 Posted August 7, 2011 Author Share Posted August 7, 2011 cANT ANYONE SOLVE THIS ISSUE???? Quote Link to comment https://forums.phpfreaks.com/topic/244127-problem-in-returning-the-last-insert-id/#findComment-1253776 Share on other sites More sharing options...
AbraCadaver Posted August 7, 2011 Share Posted August 7, 2011 PHP has a built in function for that: mysql_insert_id. If you need to use the MySQL LAST_INSERT_ID() function directly, read the manual entry for it. Your syntax is off. Quote Link to comment https://forums.phpfreaks.com/topic/244127-problem-in-returning-the-last-insert-id/#findComment-1253783 Share on other sites More sharing options...
radiations3 Posted August 7, 2011 Author Share Posted August 7, 2011 PHP has a built in function for that: mysql_insert_id. If you need to use the MySQL LAST_INSERT_ID() function directly, read the manual entry for it. Your syntax is off. How to use mysql_insert_id() to retrieve the last id from the table employee KINDLY HELP!!!??? Quote Link to comment https://forums.phpfreaks.com/topic/244127-problem-in-returning-the-last-insert-id/#findComment-1253791 Share on other sites More sharing options...
AbraCadaver Posted August 7, 2011 Share Posted August 7, 2011 OK, since it's kind of vague. The id it returns is not table specific. It is for the last successful insert query. Not foolproof, but if you need it for a specific table you might do: SELECT MAX(id) FROM employee Quote Link to comment https://forums.phpfreaks.com/topic/244127-problem-in-returning-the-last-insert-id/#findComment-1253885 Share on other sites More sharing options...
radiations3 Posted August 8, 2011 Author Share Posted August 8, 2011 OK, since it's kind of vague. The id it returns is not table specific. It is for the last successful insert query. Not foolproof, but if you need it for a specific table you might do: SELECT MAX(id) FROM employee Besides above mentioned method....Can anyone kindly guide me how can i return my last id to use mysql_insert_id() from the table employee KINDLY HELP!!!??? Quote Link to comment https://forums.phpfreaks.com/topic/244127-problem-in-returning-the-last-insert-id/#findComment-1254178 Share on other sites More sharing options...
radiations3 Posted August 8, 2011 Author Share Posted August 8, 2011 Does any body knows how to do it??? Quote Link to comment https://forums.phpfreaks.com/topic/244127-problem-in-returning-the-last-insert-id/#findComment-1254193 Share on other sites More sharing options...
TeNDoLLA Posted August 8, 2011 Share Posted August 8, 2011 You can't as already mentioned in the posts above, mysql_insert_id() returns the ID from the last query u ran in your scripts. To get the latest row in some table you should use: SELECT * FROM some_table ORDER BY id DESC LIMIT 1 Quote Link to comment https://forums.phpfreaks.com/topic/244127-problem-in-returning-the-last-insert-id/#findComment-1254195 Share on other sites More sharing options...
radiations3 Posted August 8, 2011 Author Share Posted August 8, 2011 Thanx ALL!!!! Quote Link to comment https://forums.phpfreaks.com/topic/244127-problem-in-returning-the-last-insert-id/#findComment-1254198 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.