mysql database to two dimetnional array
Moderator: General Moderators
-
chopficaro
- Forum Commoner
- Posts: 68
- Joined: Fri Jan 01, 2010 12:56 am
mysql database to two dimetnional array
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
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
thanks bro, works great!