- Code: Select all
$con = mysql_connect("localhost","peter","abc123");
die('Could not connect: ' . mysql_error());
$result = mysql_query("SELECT * FROM Persons");
while($row = mysql_fetch_array($result))
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br />";
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.