Page 1 of 1

mysql database to two dimetnional array

Posted: Thu Aug 18, 2011 7:49 pm
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?

Re: mysql database to two dimetnional array

Posted: Thu Aug 18, 2011 8:31 pm
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);

Re: mysql database to two dimetnional array

Posted: Fri Aug 19, 2011 12:23 am
by chopficaro
thanks bro, works great!