1

My php code:

$sql = mysql_query("SELECT * FROM users WHERE user_id IN (1,3)");
    while($row = mysql_fetch_array($sql)) {
    echo $row['name'];
    }

It returns as last result set, which is from user_id = 3 and does not include the result of user_id = 1 but when i print_r($row) all the result is there, is there anything amiss?

Thanks

9
  • -1 , please stop using the ancient mysql_* functions. Commented Jan 1, 2012 at 13:09
  • several examples of looping exist on stack: here's one stackoverflow.com/questions/3931219/… Commented Jan 1, 2012 at 13:11
  • Does user_id=1 actually have a name set? Are you sure its not printing an empty string. Commented Jan 1, 2012 at 13:40
  • 1
    @tereško - Many would not consider that a valid reason for a downvote. Commented Jan 1, 2012 at 13:41
  • @tereško Allow me to politely disagree. mysql_ functions are still widely used, and as far as I know, most of them are not even close to being deprecated. Not only is your downvote absurd, considering the approach is valid, you are giving misleading information. I had to +1 this. Happy new year to you and your loved ones, cheers. Commented Jan 1, 2012 at 13:46

1 Answer 1

3

If you want to access elements of array by attribute names use this:

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
   printf("ID: %s  Name: %s", $row["id"], $row["name"]);
}

else just use numeric instead of associative keys

as

echo $row[0]
Sign up to request clarification or add additional context in comments.

1 Comment

Note you can also use mysql_fetch_assoc instead.

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.