1

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??

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
Niall Paterson
  • 3,580
  • 3
  • 29
  • 37

3 Answers3

5

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'";
Dirk
  • 3,030
  • 1
  • 34
  • 40
  • Why should you use single quotes? – Lightness Races in Orbit Jul 24 '11 at 19:38
  • Actually, the quotes don't matter to mysql - it is php that doesn't replace $var with the value when enclosed in single quoted-strings. – Inca Jul 24 '11 at 19:41
  • I thought you had to, but I see you don't. – Dirk Jul 24 '11 at 19:42
  • The problem is not that he's using double quotes in his MySQL (see http://dev.mysql.com/doc/refman/5.1/en/string-syntax.html); the problem is that variables within single quotes are not evaluated by PHP. – Mike Jul 24 '11 at 19:43
  • @Mike, ok, I changed the answer, but he still has a bad SQL query. There's a comma `,` in it instead of an `AND` or `OR`. – Dirk Jul 24 '11 at 19:51
  • 1
    Your previous answer was correct and you have now made it incorrect. What was wrong was your statement saying that MySQL doesn't accept double quotes. Now you are misusing quotes and the variables $age, $town and $id will not be evaluated by PHP. – Mike Jul 24 '11 at 19:54
  • For example, this is perfectly fine for MySQL: `$sql1 = "SELECT * FROM login WHERE age= \"$age\" AND town = \"$town\" and ID != \"$id\"";` Of course, it's better to use single quotes instead of `\"` – Mike Jul 24 '11 at 19:56
  • @Mike, I see how the last statement could have been misunderstood. I did not intend for it to be taken in that manner. I was trying to say you should use single quotes while using MySQL in PHP. – Dirk Jul 24 '11 at 20:01
  • @Dirk, but using single quotes in SQL statements is not necessary. You can use single or double quotes. Using single quotes in this case simply improves readability. However, if you make the entire string single quoted, the variables won't be evaluated by PHP. They will remain as string literals. – Mike Jul 24 '11 at 20:05
3

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

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.

Mike
  • 23,542
  • 14
  • 76
  • 87