Jump to content

reg. while loop with fetch_row


kimdan
Go to solution Solved by mac_gyver,

Recommended Posts

PHP Freaks forum users,

 

Thanks in advance for your interest in helping.

 

This is my second post, I have general, basic computer programming experience and am beginning PHP.

 

I don't see why the following loop works (PHP), given the variable

$sqli1_dat 

an object taken from a mysqli query. the loop --

 

while( $x = $sqli1_dat->fetch_row() )

{

printf("%s\n", $x[0] );

}

 

this prints column 0. I am aware that fetch_row increments the pointer every time it's called.

 

But I do not follow the loop condition expression. It looks like a variable assignment, like $x = 5? But the loop condition needs to be a truth-value, such as the expression

$x == 5

 

 

Dan

Link to comment
Share on other sites

  • Solution
It looks like a variable assignment

 

 

yes, it is.

 

 

But the loop condition needs to be a truth-value

 

 

and, yes, it is. it's testing the result of the assignment statement, which will be the value that was assigned. when there are no more rows, the fetch statement returns a false, which will be assigned to the $x variable, and the value tested for the loop condition will be a false and the loop will end.

 

from the documentation for the assignment operator -

The value of an assignment expression is the value assigned. That is, the value of "$a = 3" is 3. This allows you to do some tricky things:

 

Edited by mac_gyver
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.