Selecting multiple rows from a MySQL database

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

Selecting multiple rows from a MySQL database

Post by ampakine on Sat Jun 25, 2011 4:08 pm
([msg=59015]see Selecting multiple rows from a MySQL database[/msg])

When you want to select and display multiple rows from your MySQL database I noticed a few tutorials recommend doing it like this:
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.
ampakine
Experienced User
Experienced User
 
Posts: 65
Joined: Tue May 31, 2011 5:21 pm
Blog: View Blog (0)


Re: Selecting multiple rows from a MySQL database

Post by neuromanta on Sat Jun 25, 2011 4:59 pm
([msg=59018]see Re: Selecting multiple rows from a MySQL database[/msg])

Every time the wile loop runs, the row variable gets another row from the table as it's value. That's what "$row = mysql_fetch_array($result)" does. This line used as a condition will return true while the table has another row to get, and will become false when it reaches the end. That's when the while loop will end.
User avatar
neuromanta
Poster
Poster
 
Posts: 302
Joined: Mon Nov 30, 2009 9:29 am
Location: Hungary
Blog: View Blog (0)


Re: Selecting multiple rows from a MySQL database

Post by ampakine on Sat Jun 25, 2011 11:55 pm
([msg=59033]see Re: Selecting multiple rows from a MySQL database[/msg])

What I'm confused about is how $row = mysql_fetch_array($result); can be used as a condition though. Heres my line of thinking: if the variable is undefined to begin with then it has no value so the condition where the variable equals something should only hold true if it equals something else with no value. I don't really know how loops work, I think thats where I'm getting confused.
ampakine
Experienced User
Experienced User
 
Posts: 65
Joined: Tue May 31, 2011 5:21 pm
Blog: View Blog (0)


Re: Selecting multiple rows from a MySQL database

Post by Assassian360 on Sun Jun 26, 2011 1:09 am
([msg=59040]see Re: Selecting multiple rows from a MySQL database[/msg])

If you just google the command then you get this page http://php.net/manual/en/function.mysql-fetch-array.php

Basically because the return in non-false in all cases where there is a new row, it is accepted as true and the loop begins.
Assassian360
Poster
Poster
 
Posts: 135
Joined: Sat Jun 26, 2010 1:37 am
Blog: View Blog (0)


Re: Selecting multiple rows from a MySQL database

Post by neuromanta on Sun Jun 26, 2011 1:17 am
([msg=59041]see Re: Selecting multiple rows from a MySQL database[/msg])

The '=' operator doesn't mean "equal", the equal operator is '=='. The '=' is assignment, which returns true if the assignment could be done, and returns false otherwise.
User avatar
neuromanta
Poster
Poster
 
Posts: 302
Joined: Mon Nov 30, 2009 9:29 am
Location: Hungary
Blog: View Blog (0)


Re: Selecting multiple rows from a MySQL database

Post by Assassian360 on Sun Jun 26, 2011 6:04 am
([msg=59049]see Re: Selecting multiple rows from a MySQL database[/msg])

neuromanta wrote:The '=' operator doesn't mean "equal", the equal operator is '=='. The '=' is assignment, which returns true if the assignment could be done, and returns false otherwise.


That is not completely correct based on the way you worded it. See http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting.
The value stored in the variable is evaluated as a boolean based on the rules that are in the PHP manual.
Assassian360
Poster
Poster
 
Posts: 135
Joined: Sat Jun 26, 2010 1:37 am
Blog: View Blog (0)


Re: Selecting multiple rows from a MySQL database

Post by ampakine on Sun Jun 26, 2011 8:29 am
([msg=59052]see Re: Selecting multiple rows from a MySQL database[/msg])

neuromanta wrote:The '=' operator doesn't mean "equal", the equal operator is '=='. The '=' is assignment, which returns true if the assignment could be done, and returns false otherwise.

Oh yeah I was getting it mixed up with ==. Thanks.
ampakine
Experienced User
Experienced User
 
Posts: 65
Joined: Tue May 31, 2011 5:21 pm
Blog: View Blog (0)


Re: Selecting multiple rows from a MySQL database

Post by neuromanta on Sun Jun 26, 2011 10:05 am
([msg=59054]see Re: Selecting multiple rows from a MySQL database[/msg])

Assassian360 wrote:
neuromanta wrote:The '=' operator doesn't mean "equal", the equal operator is '=='. The '=' is assignment, which returns true if the assignment could be done, and returns false otherwise.


That is not completely correct based on the way you worded it. See http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting.
The value stored in the variable is evaluated as a boolean based on the rules that are in the PHP manual.


You're right. My PHP knowledge has lost it's shine in the past few years :D.
User avatar
neuromanta
Poster
Poster
 
Posts: 302
Joined: Mon Nov 30, 2009 9:29 am
Location: Hungary
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests