简体   繁体   中英

problem with mysql_num_rows

Here is my code :

$sql1 = 'SELECT * FROM login WHERE age= "$age", town = "$town" and ID != "$id"';
$result1 = mysql_query($sql1);
$numResults1 = mysql_num_rows($result1);

My variables are fine, they have data in them. The error is this :

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

There is a possibility that numResults could equal 0 but it still should not cause this.

Could it be the != in the first line that is causing it??

The problem is you have a bad SQL query:

$sql1 = 'SELECT * FROM login WHERE age= "$age", town = "$town" and ID != "$id"';

Note the improper , after age = "$age" in the WHERE clause. It should be something like:

$sql1 = "SELECT * FROM login WHERE age= '$age' AND town = '$town' and ID != '$id'";

Your query has a syntax error. Check $result1 before attempting to use it; print mysql_error() when it's FALSE . You'll be told what the problem is.

Also, your use of single quotes for the SQL query string means that variable interpolation will not occur... so you are unlikely to ever get rows returned.

You're getting your single and double quotes mixed up. Make all single quotes double quotes and vice versa.

Try echoing $sql1 to see what I mean

There is a possibility that numResults could equal 0 but it still should not cause this.

No. It means you have a problem with your SQL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM