Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 30th, 2012, 06:26 PM
Registered User
 
Join Date: Jan 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default how does while($row = mysql_fetch_array) work?

Hello all,
I'm new in php and i have a question.

Assume we have the following code (from Wrox Beg php6 web development)

while ($row = mysql_fetch_array($result)) {

extract($row);


echo $movie_name . ' - ' . $movie_type . '<br />';
}

I would like to explain me how does while work? at first the $row gets the value of mysql_fetch_array($result) ? and after that? what value of $row does stop the loop? perhaps it is a dummie question but i can't understand it....


Thank you

Last edited by Stamatis; January 30th, 2012 at 06:29 PM..
 
Old January 31st, 2012, 07:54 AM
Friend of Wrox
 
Join Date: May 2011
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

The loop ends when $row is false, i.e. no array is returned from the db.

So if you had a table with 5 rows you would get 5 results back once the line;
Code:
while ($row = mysql_fetch_array($result))
tries to get row 6 you get nothing back and the loop ends.
 
Old January 31st, 2012, 08:23 AM
Registered User
 
Join Date: Jan 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi useless and thanks for your reply...
However i cannot understand how the $row gets the value.The = in php is set of a variable or an equality? is there the == in the while or not? because for example in C if we have the same example we have an infitite loop. here is difference?

Perhaps i have not understand how the set of variables work in php loops

Sorry for this dummie questions but i try to understand

Thank you
 
Old January 31st, 2012, 10:06 AM
Friend of Wrox
 
Join Date: May 2011
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

= assigns variable on left the value on the right.
== determines equality
=== determines equality AND type

So you'd use = like so;
Code:
$somevar = 'A string variable';
you'd then use == like so;
Code:
if( $somevar == 'A string variable' )
{
echo 'Match found';
}
else
{
echo 'Match NOT found';
}
and finally;
Code:
if( ($pos = strpos($somevar, 'string')) !== false )
{
	echo 'Found "string" at position: ' . $pos . ' in string: "' . $somevar .'"';
}
 
Old January 31st, 2012, 11:30 AM
Registered User
 
Join Date: Jan 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

I can understand that you writed but i can't understand how the = work in the while.

while ($row = mysql_fetch_array($result))

Regarding the adove you writed in your post (
= assigns variable on left the value on the right.) the $row at first get the value of mysql_fetch_array($result).... so... when it gets the same value how the loop works and we don't have a infinite loop, when every time has the value of the "mysql_fetch_array($result)" For example it the same with while($row = 5)... so there is an infinite loop here.


Thanks and sorry again

Last edited by Stamatis; January 31st, 2012 at 11:37 AM..
 
Old January 31st, 2012, 04:12 PM
Friend of Wrox
 
Join Date: May 2011
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

Like I said in my previous post as long as $row is not false then the loop will continue to loop, once $row is false (like when nothing is returned from the db) then the loop ends.

Or put another way as long as the expression, in this case $row = mysql_fetch_array($result), is true then the while loop will continue to loop. The loop ends when the expression evaluates to false.

More info here: http://www.php.net/manual/en/control...ures.while.php
 
Old January 31st, 2012, 05:51 PM
Registered User
 
Join Date: Jan 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok I understand now!

Thanks a lot!

Best regards





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chp 12 : mysql_fetch_array doesn't return any array DMatt BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 0 August 6th, 2010 10:31 AM
mysql_fetch_array and if .. else Jeff Smitherman Beginning PHP 3 November 21st, 2005 09:08 AM
Problem with mysql_fetch_array insomne PHP Databases 0 September 27th, 2004 02:03 PM
mysql_fetch_array with a variable string aeres PHP How-To 7 August 18th, 2003 01:01 PM
mysql_fetch_array problem... aeres PHP Databases 2 July 4th, 2003 12:10 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.