"KDawg44" <KD*****@gmail. comwrote in message
news:01******** *************** ***********@a1g 2000hsb.googleg roups.com...
Hi,
Is there a way to get a multidimensiona l associative array with the
entire result set? I would like to get a an array like this:
resultsArray['TableKey']['columnsInTable ']
How can I accomplish this? Can I do something like this?
var $userArray = array(array());
for ($i=0; $i<$numResults; $i++) {
$row = mysql_fetch_arr ay($resultSet, MYSQL_ASSOC);
$userArray[$row['Key']] = $row;
}
This is not correct but I am hoping it is at least a starting point.
it's not bad. what i would say in looking at it is that you only need to
initialize $userArray = array()...the inner, empty array doesn't allocate
space...it's not needed. that said, here's what i use in a mysql
implementation of an abstract db class:
public static function execute(
$sql ,
$decode = false ,
$returnNewId = false
)
{
self::$lastStat ement = $sql;
$array = array();
$key = 0;
if (!($records = mysql_query($sq l))){ return false; }
$fieldCount = @mysql_num_fiel ds($records);
while ($row = @mysql_fetch_ar ray($records, MYSQL_NUM))
{
for ($i = 0; $i < $fieldCount; $i++)
{
$value = $row[$i];
if ($decode){ $value = self::decode($v alue); }
$array[$key][strtoupper(@mys ql_field_name($ records, $i))] = $value;
}
$key++;
}
if ($returnNewId)
{
$array = array();
$array[0]['ID'] = mysql_insert_id ();
}
@mysql_free_res ult($records);
return $array;
}
forget the 'decode' stuff since i didn't post the function. anyway, that
would be the basis for returning a single resultset. to multi-dimension it,
just make a key for the table:
$myTables['TABLE_A'] = db::execute($sq l);
$myTables['TABLE_B'] = db::execute($sq l);
$myTables['TABLE_C'] = db::execute($sq l);
just assume that $sql reflects the proper query for each 'table'...meani ng,
i'm not showing in the example above that $sql represents a new query for
each 'execute'.
hope that helps...fwiw, you're already on the right track.
cheers.