0

I'm trying to run a while loop from a MySQL query (which I've done a ton of times) and I can't get it to run for some reason. If I run the code below, I get 'test' echoed out but not 'test2' even though there are entries in the database that match the criteria in my SQL query. I guess I'm just trying to see if I'm overlooking something in the syntax?

I have the database connect files included on the page with 'require' statements so the page wouldn't even run if it was a mistake when connecting to the database.

Any ideas? I'm sorry if this is basic, it's just been driving me crazy this morning. Thank you for your help!

$sql_all = "SELECT * FROM wp_posts WHERE post_type = 'post' AND post_status != 'trash' AND post_status != 'auto-draft'";
$result_all = $mysqli->query($sql_all);

echo "test";

while($row_all == mysqli_fetch_array($result_all)){
    echo "test2";    
}
1
  • Do you see any errors? What do you see if you do echo $mysqli->error;? Maybe the query returns 0 rows? What do you see if you do echo $result_all->num_rows? Commented Feb 11, 2014 at 18:05

1 Answer 1

4

Take out the second equal sign in your while loop.

while($row_all = mysqli_fetch_array($result_all)){
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, as strange is it may look, this is correct. You are assigning the value returned by mysqli_fetch_array, then checking if it's "truthy".
Really? Wow, I learn something new every day! Thanks for the tip, Rocket.
I was just expanding on your answer ;)
Had it been a "conditional statement", then it would be a different story. Since it's a while (loop), then the single = sign must be applied as "equaling to" and not "assigned to".

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.