Jump to content

[SOLVED] checking if record exists - why doesn't this work?!


Recommended Posts

Hi,

 

I'm trying to find out if a record exists in a table using the following code but keep getting this error:

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in

 

Any ideas what I might be doing wrong!?

 

Cheers

 

Simon

 


$ipaddress = $_SERVER['REMOTE_ADDR']; 

  // Make the connection
  $db2 = mysql_connect($myHost2, $myUser2, $myPass2);

  // Select the database
  mysql_select_db($myDB2,$db2);

  $sql2 = "mysql_query(SELECT ipaddress FROM $myTable2 WHERE ipaddress = $ipaddress)";

  // Get the query as an associative array
  $result2=mysql_query($sql2,$db2);

  // Get the ID number of the new record
  $recordID2 = mysql_insert_id();

  // Close the database  connection
  mysql_close($db2);

  $num_rows = mysql_num_rows($sql2);
  echo "output:  $num_rows";

  • 2 weeks later...

Thanks dropfaith - I'm now seeing the error:

 

Error in query: SELECT ipaddress FROM 'users' WHERE ipaddress = '217.43.120.4'. 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 ''users' WHERE ipaddress = '217.43.120.4'' at line 1

 

Any ideas? The SQL looks good to me!

 

Cheers

 

Simon

Your mistake was not putting quotes around $ipaddress.

dilum's mistake was putting quotes around the table name.

And you don't need to make a mess of your code breaking it with dots and quotes in the way you finally did.

Best of that, you'd better take the habit to test ALL of your database commands, this way:

 

$db2 = mysql_connect($myHost2, $myUser2, $myPass2) or die(mysql_error());
mysql_select_db($myDB2,$db2) or die(mysql_error());
$result2=mysql_query($sql2,$db2) or die(mysql_error());
mysql

 

And remember: no quotes around tables or columns names. Just backticks, if you need them. At the opposite, always quotes around columns values. Quite simple  ;)

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.