0

Possible Duplicate:
mysql_num_rows(): supplied argument is not a valid MySQL result resource

            $resultnx = mysql_query("SELECT * FROM `emails` WHERE `email` = '{$email}'");
        $num_rows = mysql_num_rows($resultnx);

gets this warning Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in ....

Community
  • 1
  • 1
user355367
  • 21
  • 1
  • I should mention that the prior code puts at least 1 instance in the database. Its supposed to help me check for duplicates. – user355367 Jun 01 '10 at 12:02
  • This code seems vulnerable to SQL injection. Use a prepared statement instead. – Tgr Jun 01 '10 at 12:32

6 Answers6

4

Looks like there is some error in your query, use the die to see what error it is:

 $query = "SELECT * FROM `emails` WHERE `email` = '{$email}'";
 $resultnx = mysql_query($query) or die(mysql_error());
 $num_rows = mysql_num_rows($resultnx);

Also, check to make sure that $email is coming through fine:

var_dump($email);
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • @user355367: Is that the error you receive? – Sarfraz Jun 01 '10 at 12:10
  • Yes. The script runs for a while then it drops that. its holding the variables fine. – user355367 Jun 01 '10 at 12:13
  • @user355367: It means the mysql server is down, you could either wait or contact server support team and inform them about the error. – Sarfraz Jun 01 '10 at 12:14
  • Im running the script from a database created by root on a non root account on a vps. Could this make a difference? – user355367 Jun 01 '10 at 12:19
  • @user355367: I don't think so, it looks like mysql is down, check any other site refering to same mysql server to see if it works. – Sarfraz Jun 01 '10 at 12:25
  • @user355367: It is very unlikely that that is the case. Check the server error logs to see why MySql keeps crashing. – Joseph Jun 01 '10 at 14:46
0

Or try

 $query = "SELECT * FROM `emails` WHERE `email` = '".$email."'";
 $resultnx = mysql_query($query) or die(mysql_error());
 $num_rows = mysql_num_rows($resultnx);
Salil
  • 46,566
  • 21
  • 122
  • 156
0

Try outputting the contents of $query and then pasting it into some query execution tool.

James Wiseman
  • 29,946
  • 17
  • 95
  • 158
0

This doesn't answer your question directly, but there are probably better ways to check for duplicates. Consider modifying the email field to be unique. From your mySQL command line (or PHPMyAdmin) do the following:

ALTER TABLE `emails` ADD UNIQUE(`email`);

Now if you try to insert a row where the email already exists it will throw an error for you all on its own.

Erik
  • 20,526
  • 8
  • 45
  • 76
0

I think you need to remove curly braces from $email.

$query = "SELECT * FROM `emails` WHERE email = '$email'";
$resultnx = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($resultnx);
RAS
  • 8,100
  • 16
  • 64
  • 86
VATSHAL
  • 1,529
  • 2
  • 12
  • 12
0

The error you are receiving directly

MySQL server has gone away

is definitely caused by the MySql server being down. If the MySql Server and the PHP script are on the same VPS, then you are going to need to hunt down exactly what is causing the database to shut down (crash). Check the server error logs. They should tell you what the problem is.


Instead of performing the query the way you have written, why don't you simply do the following query instead:

$query = "SELECT count(*) FROM `emails` WHERE `email` = '".$email."'";

That way you can just pull the number of rows rather than all the data which runs quite a bit faster on MySql (especially if email is indexed).

$query = "SELECT count(*) FROM `emails` WHERE `email` = '".$email."'";
$resultnx = mysql_query($query) or die(mysql_error());
$num_rows_array = mysql_fetch_array($resultnx);
$num_rows = $num_rows_array[0];

Lines in the code can be combined. They were separated to make it easier to see what was occurring.

Joseph
  • 1,988
  • 14
  • 21