stevieontario Posted May 24, 2009 Share Posted May 24, 2009 Hello PHP Freaks: I'm playing in a xampp sandbox and trying to insert the last auto-incremented id of one table into another table. I am using the function mysql_insert_id(), in the following way: $lastSid = mysql_insert_id($db_connection); ... and here is the database connection code from my config file: function sqlquery($myquery,$sets=0) { global $db_hostname,$db_name,$db_username,$db_password; $db_connection = mysql_connect($db_hostname, $db_username, $db_password) or die("Could not connect: " . mysql_error()); mysql_select_db($db_name, $db_connection); $results = mysql_query($myquery, $db_connection) or die(mysql_error()); switch ($sets) { case 1: $sqlres = mysql_num_rows($results); break; case 2: $sqlres = mysql_fetch_array($results); break; default: $sqlres = $results; } I keep getting the error "supplied argument is not a valid MySQL-Link resource." I have checked the db connection, and everything seems fine. (The other queries I have run work.) Any ideas what I've done wrong? Mysql version 5.1.30 PHP version 5.2.8 Quote Link to comment https://forums.phpfreaks.com/topic/159493-msql_insert_id-problem-help/ Share on other sites More sharing options...
Maq Posted May 25, 2009 Share Posted May 25, 2009 mysql_insert_id takes a resource id, not a database connection. Quote Link to comment https://forums.phpfreaks.com/topic/159493-msql_insert_id-problem-help/#findComment-841461 Share on other sites More sharing options...
PFMaBiSmAd Posted May 25, 2009 Share Posted May 25, 2009 mysql_insert_id() actually does optionally take a link identifier. The posted code would have worked if you had left out the optional link identifier because it would have used the existing client connection to the database server. The posted code does not work because you have a scope problem with $db_connection. It only exists inside of the sqlquery function. Quote Link to comment https://forums.phpfreaks.com/topic/159493-msql_insert_id-problem-help/#findComment-841637 Share on other sites More sharing options...
stevieontario Posted May 25, 2009 Author Share Posted May 25, 2009 Thanks Maq and PFMaBiSmAd, Should have mentioned that I tried leaving the argument blank -- i.e. calling mysql_insert_id() -- but received another warning, which was: "... A link to the server could not be established in [filepath/filename]" For some reason I thought the first warning was more important. Anyway, I tried making $db_connection global, but got the original warning. If you have any suggestions, I appreciate your time! Quote Link to comment https://forums.phpfreaks.com/topic/159493-msql_insert_id-problem-help/#findComment-841661 Share on other sites More sharing options...
stevieontario Posted May 25, 2009 Author Share Posted May 25, 2009 Correction on my previous reply: I said "Anyway, I tried making $db_connection global, but got the original warning." Not exactly right. The warning actually reads "9 is not a valid MySQL-Link resource in [filepath/filename]" Quote Link to comment https://forums.phpfreaks.com/topic/159493-msql_insert_id-problem-help/#findComment-841662 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.