- Code: Select all
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons");
while($row = mysql_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br />";
}
mysql_close($con);
?>
I understand that mysql_fetch_array selects 1 row at a time from the database and thus putting it in a loop will sequentially select every row in the table but what I don't get is how $row = mysql_fetch_array($result) holds true until it reaches the last row of the table. The $row variable isn't defined at all so wouldn't it be empty? In other words wouldn't mysql_fetch_array($result) have to have no value until the last row of the table is reached then gain a value in order for the loop to end? If I echo mysql_fetch_array it prints "Array" so it has a value. I'm fairly confused here.


