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

3
  • != and <> are both valid MySQL not equal operators. Commented Jul 24, 2011 at 19:32
  • 3
    I don't like that your SO username is an advertisement. Commented Jul 24, 2011 at 19:40
  • You can see MySQL errors if you change the second line to $result1 = mysql_query($sql1) or die( mysql_error() ); Commented Jul 24, 2011 at 19:41

3 Answers 3

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'";
Sign up to request clarification or add additional context in comments.

9 Comments

Why should you use single quotes?
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.
I thought you had to, but I see you don't.
The problem is not that he's using double quotes in his MySQL (see dev.mysql.com/doc/refman/5.1/en/string-syntax.html); the problem is that variables within single quotes are not evaluated by PHP.
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.
|
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.

Comments

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.

2 Comments

Seriously, you are. $var = 3; echo '$var'; will echo literally $var instead of 3.
@Backslap: Why do you say that? (a) Yes, you do; (b) Your code doesn't even check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.