Traversing a query result - mysql_fetch_array etc.
Posted: Thu Jun 27, 2002 11:38 pm
Hi,
I'm kind of... well am... a beginner to php and mysql although I've had some experience programming and other various computer related fields.
Right now I'm trying to utilize both and was wondering if someone might clear a few things up for me.
In this code (I've added the line numbers for reference):
01 $result = mysql_query("SELECT id, name FROM mytable");
02
03 while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
04 printf ("ID: %s Name: %s", $row["id"], $row["name"]);
05 }
06 mysql_free_result($result);
In line 01 I understand that the database query's mytable and returns the columns id and name for all rows and sets $result to it.
What I'm a little unsure of is exactly how line 03 works. Basically I understand that mysql_fetch_array() checks to see if there is a row existing in from $result if so it takes that row and set's an array called $row to it if it doesn't it returns false and the while loop ends.
I don't, however, understand exactly how mysql _fetch_array traverses $result. At a basic level I see that it pulls the first row out, then moves to the next one, and so on and so forth, but what exactly is going on here? How does it determine what row it is currently on or is it modifying $result by removing the top row?
Is $result simply a multidimensional array or is something else going on here?
Also while it's on my mind (and can't check the obvious at the moment) is there a way to traverse through $result in such an multidimensional manner? eg. $result(1,3)
I've been able to program ok without knowing these answers so far, but I'd like to know exactly what kind of objects and functions I'm dealing with and how they work so I can avoid future problems and code more efficiently.
Thanks
I'm kind of... well am... a beginner to php and mysql although I've had some experience programming and other various computer related fields.
Right now I'm trying to utilize both and was wondering if someone might clear a few things up for me.
In this code (I've added the line numbers for reference):
01 $result = mysql_query("SELECT id, name FROM mytable");
02
03 while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
04 printf ("ID: %s Name: %s", $row["id"], $row["name"]);
05 }
06 mysql_free_result($result);
In line 01 I understand that the database query's mytable and returns the columns id and name for all rows and sets $result to it.
What I'm a little unsure of is exactly how line 03 works. Basically I understand that mysql_fetch_array() checks to see if there is a row existing in from $result if so it takes that row and set's an array called $row to it if it doesn't it returns false and the while loop ends.
I don't, however, understand exactly how mysql _fetch_array traverses $result. At a basic level I see that it pulls the first row out, then moves to the next one, and so on and so forth, but what exactly is going on here? How does it determine what row it is currently on or is it modifying $result by removing the top row?
Is $result simply a multidimensional array or is something else going on here?
Also while it's on my mind (and can't check the obvious at the moment) is there a way to traverse through $result in such an multidimensional manner? eg. $result(1,3)
I've been able to program ok without knowing these answers so far, but I'd like to know exactly what kind of objects and functions I'm dealing with and how they work so I can avoid future problems and code more efficiently.
Thanks