Jump to content

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


simonp

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  ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.