1

I can't figure out why this keeps breaking my page. Could someone take a look? Thanks!

while ($row = mysql_fetch_array(mysql_query("SELECT * FROM `mytable` WHERE `col1` = 0"))) {
    echo $row['id'];
}

I've seen this type of while loop show up pretty regularly in google searches and just browsing through stackoverflow. I don't know why it isn't working for me.

How would I achieve the desired result? (Echo the id of each row where col1 = 0)

2 Answers 2

6

Obviously it is because you create new MySQL resource with every iteration and then use it's first row.

Use something like

$res = mysql_query("...");
while ($row = mysql_fetch_array($res)) {
    echo $row['id']'
}

By the way, you do know that mysql is deprecated, do you not?

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

Comments

0
$result = mysql_query("SELECT * FROM `mytable` WHERE `col1` = 0");
while ($row = mysql_fetch_array($result)) {
    echo $row['id'];
}

Comments

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.