Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/159493-msql_insert_id-problem-help/
Share on other sites

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.

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.