1

I've been stuck on this concept for about 5 hours now, and it's really frustrating me.

$result = $mysqli->query("SELECT col FROM table");
while($row = $result->fetch_assoc()){
    echo $row['data'];
}

I understand that the function only fetches one row at a time, but I don't understand how it's being called when the loop resets. How is it being called in $row = $result->fetch_assoc? Also, how does this condition evaluate to true if $row is null?

4
  • What do you mean by "when the loop resets"? A while loop does not reset. Also what do you mean by "how does this condition evaluate to true if $row is null?"? Because if $row is NULL it does not evaluate to true, so you should perhaps define what the condition is you ask about. Commented Sep 3, 2013 at 5:44
  • And also I'd like to know which parts of the PHP manual have you looked into to get more understanding of this issue. It should be explained and if you got problems to navigate it, you perhaps needs some help with that. Commented Sep 3, 2013 at 5:45
  • you must print_r($row) to see its structure Commented Sep 3, 2013 at 5:46
  • I realized that the $row variable was set in the while statement, so $row isn't actually null Commented Sep 3, 2013 at 20:30

5 Answers 5

1

Okay here is simple test for you,

let have an array which has value which is null as demonstrated below,

$row = array('value' => null);

and now lets check for that using if condition

if($row)
{
    echo "null is making variable value to true so condition will work.";
}

paste code and run it i am sure that you will see message inside of if condition.

Your Condition

You are using $result->fetch_assoc() so as you know it will return array which might have null value as demonstrated in above example.

but as you see it will return true for the $result because $result actually has value assigned and it is true.

so condition will be satisfied.

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

Comments

1

In short the while loop condition is looking for true:

while(true){
//do this
}

So until this expression $row = $result->fetch_assoc() resolves to true the while loop will continue.

The question is when it will be not true? when all rows retrieved.

1 Comment

That is really what it is, simple as that
0

how does this condition evaluate to true if $row is null?

It doesn't. That's the point.

Comments

0

Given your code:

while($row = $result->fetch_assoc()){
    echo $row['data'];
}

The loop condition can be rewritten to the following equivalent:

while (($row = $result->fetch_assoc()) != false) {
    echo $row['data'];
}

In other words, while $row is not falsy, it will continue the loop; null is also considered falsy.

Comments

-1

the following explanation to your code may help you

// run the query. this returns a mysqli_result object on success.
// on failure query function returns false and you have to handle this condition.
$result = $mysqli->query("SELECT col FROM table");

// fetch_assoc() here you fetch one row at a time from the mysqli_result
// object into $row. this also moves the pointer to the next row
// so the next call returns the next row.
// This returns false when there is no more rows and exit your while loop
while($row = $result->fetch_assoc()){
    echo $row['data'];
}

links you may want to refer
http://www.php.net/manual/en/class.mysqli-result.php
http://php.net/manual/en/mysqli-result.fetch-assoc.php

1 Comment

the downvoter!! can you explain what is wrong with the answer?

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.