mysql database to two dimetnional array

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
chopficaro
Forum Commoner
Posts: 68
Joined: Fri Jan 01, 2010 12:56 am

mysql database to two dimetnional array

Post by chopficaro »

so i know how to grab a row from a mysql database with mysql_fetch_array, but how do i turn the whole database into a two dimensional array so i can close the connection and just work with my array?
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: mysql database to two dimetnional array

Post by twinedev »

You could do something like the following. It assumes you have a single PrimaryKey.

Code: Select all

	$aryProduct = array();
	$strPrimaryKey = 'ProductID';
	$SQL = 'SELECT * FROM tblProduct';

	$rsProduct = mysql_query($SQL);
	if ($rsProduct && mysql_num_rows($rsProduct)>0) {
		while ($aryTemp = mysql_fetch_assoc($rsProduct)) {
			$aryProduct[$aryTemp[$strPrimaryKey]] = $aryTemp;
			// Next Line not necessary, but my preference to remove duplicate data for row 
			unset($aryProduct[$aryTemp[$strPrimaryKey]][$strPrimaryKey])
		}
		mysql_free_result($rsProduct);
	}
	unset($rsProduct,$strPrimaryKey,$SQL,$aryTemp);
chopficaro
Forum Commoner
Posts: 68
Joined: Fri Jan 01, 2010 12:56 am

Re: mysql database to two dimetnional array

Post by chopficaro »

thanks bro, works great!
Post Reply