Page 1 of 1

How to build array from select statement?

Posted: Sat Mar 31, 2007 2:51 pm
by SmokyBarnable
I'm using a select statement that is returning two rows. I want to put those rows into an array, but my code only creates the current array that is in the loop and when it goes through the loop the second time the first array is replaced in the $array variable by the second row. How can I store all rows into an array?

Code: Select all

$result = mysql_query("SELECT stuff
			FROM tables
			WHERE this = that");
			while ($row = mysql_fetch_array($result)) {
			         $array = $row;
			}

Thanks.

Posted: Sat Mar 31, 2007 3:07 pm
by nickvd

Code: Select all

//before loop
$array = array();
//in loop
$array[] = $row

Posted: Sat Mar 31, 2007 3:08 pm
by timvw
you can append to a row as following:

Code: Select all

while($row = ... ) {
$array[] = $row;
}
(If you look at the section about arrays in the php manual you'll see that there are other ways too...)

Posted: Sat Mar 31, 2007 3:23 pm
by SmokyBarnable
:)