Jump to content

I'm learning PHP and there is a piece of code that I don't completely understand


Recommended Posts

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>";

}

 

?>

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.

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.

//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.

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.