1

I want to query a MySQL database. I have the following. $name keeps changing in a loop. ($query is a sample query here)

$query = "SELECT id FROM table1 WHERE name='$name';";
$result = mysql_query($query) or die(mysql_error());        
echo "$result<br/>";
While ($row = mysql_fetch_array($result)) {
     echo $row["id"] . " - " . $row["name"];
}

with the echo "$result<br/>" it just prints something like Resource id #. Nothing from $row is printed. The MySQL connection is fine. If I run the query without PHP, it works fine. What might be wrong?

0

1 Answer 1

1

Everything is correct, you'll get nothing from $result until you use some function like mysql_fetch_array() or mysql_fetch_assoc() like you do in your loop.

Sign up to request clarification or add additional context in comments.

4 Comments

The while loop does not print anything. Is there anything that I might be missing?
$row["name"] doens't exist in your result because you only selected 'id' in your query. If your error reporting is off, the echo won't print. Try without the $row["name"]?
Should print the id anyway. I guess the query doesn't return any result (empty $name in query?). Try echo mysql_num_rows($result), should be > 0 if the query is correct.
When I echo mysql_num_rows($result), I get all 0. I do not understand. There query is correct and runs fine otherwise. Why is it not retrieving anything in this case???

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.