How to build array from select statement?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
SmokyBarnable
Forum Contributor
Posts: 105
Joined: Wed Nov 01, 2006 5:44 pm

How to build array from select statement?

Post 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.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Code: Select all

//before loop
$array = array();
//in loop
$array[] = $row
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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...)
User avatar
SmokyBarnable
Forum Contributor
Posts: 105
Joined: Wed Nov 01, 2006 5:44 pm

Post by SmokyBarnable »

:)
Post Reply