mousico Posted December 15, 2009 Share Posted December 15, 2009 I took the code below from a PHP book I'm reading and I understand how it all works except 1 point that confuses me a little - I don't get how the line "while ($rows=mysql_fetch_array($results))" works. At the point in the program that $rows is tested as to whether it is equal to the recordset which is mysql_fetch_array($results), there is surely nothing in $rows yet as nothing has been assigned to it yet so how is this condition satisfied? Also, mysql_fetch_array obviously takes only one record from the recordset "$results" but does it automatically move to the next record in $results with every iteration of this loop? <?php //connect to MySQL $connect = mysql_connect("host", "user", "password") or die ("Hey loser, check your server connection."); //make sure we're using the right database mysql_select_db ("database"); $query="SELECT movie_name, movie_type FROM movie WHERE movie_year>1990 ORDER BY movie_type"; $results=mysql_query($query) or die(mysql_error()); while ($rows=mysql_fetch_array($results)) { extract($rows); echo $movie_name; echo " - "; echo $movie_type; echo "<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/185194-im-learning-php-and-there-is-a-piece-of-code-that-i-dont-completely-understand/ Share on other sites More sharing options...
JonnoTheDev Posted December 15, 2009 Share Posted December 15, 2009 while ($rows=mysql_fetch_array($results))" works. At the point in the program that $rows is tested as to whether it is equal to the recordset which is mysql_fetch_array($results), This is not true. The = operator assigns a value, it is not a comparison. You are thinking of ==. The mysql_fetch_array() function returns an array of data from the database query for each row. As there are more than 1 rows returned, in order to iterate through them the while loop is used. So in simple words each row from the database is assigned to the variable $rows (should have used $row really) as an array until the end of the result set is reached. Quote Link to comment https://forums.phpfreaks.com/topic/185194-im-learning-php-and-there-is-a-piece-of-code-that-i-dont-completely-understand/#findComment-977654 Share on other sites More sharing options...
mousico Posted December 15, 2009 Author Share Posted December 15, 2009 Thanks for your explanation. I understand it now I think. You're right, I was comparing it to something like "While ($count == 1)" but I get it now - the assignment happens with each execution of the while line and returns a boolean of true until there are no more records? I think. Quote Link to comment https://forums.phpfreaks.com/topic/185194-im-learning-php-and-there-is-a-piece-of-code-that-i-dont-completely-understand/#findComment-977658 Share on other sites More sharing options...
JonnoTheDev Posted December 15, 2009 Share Posted December 15, 2009 correct Quote Link to comment https://forums.phpfreaks.com/topic/185194-im-learning-php-and-there-is-a-piece-of-code-that-i-dont-completely-understand/#findComment-977659 Share on other sites More sharing options...
iJoseph Posted December 15, 2009 Share Posted December 15, 2009 //connect to MySQL $connect = mysql_connect("host", "user", "password") or die ("Hey loser, check your server connection."); //make sure we're using the right database mysql_select_db ("database"); That isn't right is it? I thought it had to be... (Note the $conn in the database connection.) $conn = @mysql_connect( "host", "username", "password" ) or die ( "This code fails, Fix it." ); $rs = @mysql_select_db( "Database", $conn ) or die ( "This code fails, Fix it." ); Then again, I'm probally wrong. But that's what I always do. Quote Link to comment https://forums.phpfreaks.com/topic/185194-im-learning-php-and-there-is-a-piece-of-code-that-i-dont-completely-understand/#findComment-977884 Share on other sites More sharing options...
JonnoTheDev Posted December 15, 2009 Share Posted December 15, 2009 you are wrong Quote Link to comment https://forums.phpfreaks.com/topic/185194-im-learning-php-and-there-is-a-piece-of-code-that-i-dont-completely-understand/#findComment-977910 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.