Jump to content

[SOLVED] mysql_num_rows($sql) > 0 -- but also not a valid resource?


jeffrydell

Recommended Posts

Not quite sure how I'm getting THIS warning message:

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in E:\...\script.php on line 796

 

From THIS code:

 

			
$sql = @mysql_query("SELECT `owner_id` FROM `owners` WHERE `email` = '" . $Owner['email'] . "' AND `zipcode` = '" . $Owner['zipcode'] . "'");
		if (mysql_num_rows($sql) > 0)
		{
			$row = mysql_fetch_assoc('$sql');
			$Owner['owner_id'] = $row['owner_id'];
		}

 

In order to 'get in' the IF, doesn't there have to be at least one row matching the query string?  And if there IS one row, how can I get a message saying it's not a valid resource?

 

Thanks for whatever help you can offer!

Link to comment
Share on other sites

That does seem strange.

Put an or die(mysql_error()); at the end of your query to see what the problem is.

$sql = @mysql_query("SELECT `owner_id` FROM `owners` WHERE `email` = '" . $Owner['email'] . "' AND `zipcode` = '" . $Owner['zipcode'] . "'") or die(mysql_error());

Link to comment
Share on other sites

To elaborate on what BlueSkyIS said, variables in a string are only parsed when enclosed with double quotes (same goes for control characters like \n or \t). Also, in this case (since you don't need any other data), you'd just pass the variable directly instead of putting it in a string.

Link to comment
Share on other sites

Thanks - removing the quotes did it.  I don't know how many times I've set up that type of scenario and never put those quotes in before ... guess it goes with being human.

 

Here's a similar issue ... the array element $Dog['dog_id'] is null - I know, I tested for that:

 

if ($Dog['dog_id'] !== "")
	{
		$sql = mysql_query("SELECT * FROM `dogs` WHERE `dog_id` = " . $Dog['dog_id']) or die(mysql_error());
		$row = mysql_fetch_array($sql);       // Line 838
		$Dog['breed'] = $row['breed'];
		$Dog['sex'] = $row['sex'];
		$Dog['birthdate'] = dateconvert($row['birthdate'], 2);
	}

 

... as you can see, I already tried the "or die()" method of troubleshooting and got:

 

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

 

(MySQL's usual 'ever so helpful' error messaging.)

 

Prior to that, the error message from php was:

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in E:\...\script.php on line 838

 

... which indicates to me that the IF() evaluated true, even though it SHOULD have been false because of !=="";

 

Anyone????

 

Link to comment
Share on other sites

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.