|
Posted by Steve on 04/20/07 04:22
"Karl Groves" <karl@NOSPAMkarlcore.com> wrote in message
news:Xns9917F1554CA37karlkarlcorecom@199.45.49.11...
| I'm missing something very obvious, but it is getting late and I've
| stared at it too long.
it is obvious to me only because i have programmed it already...here's a
static method of my db class:
public static function execute($sql, $decode = false, $returnNewId =
false)
{
self::$lastStatement = $sql;
$array = array();
$key = 0;
if (!($records = mysql_query($sql))){ return false; }
$fieldCount = @mysql_num_fields($records);
while ($row = @mysql_fetch_array($records, MYSQL_NUM))
{
for ($i = 0; $i < $fieldCount; $i++)
{
$value = $row[$i];
if ($decode){ $value = self::decode($value); }
$array[$key][strtoupper(@mysql_field_name($records, $i))] = $value;
}
$key++;
}
if ($returnNewId)
{
$array = array();
$array[0]['ID'] = mysql_insert_id();
}
@mysql_free_result($records);
return $array;
}
notice, and here's what your code is missing, the use of $key. that way you
create a multi-dimentional array of fields AT $key rather than what you're
getting now. here's example of useage:
$sql = "
SELECT Foo ,
Bar
FROM raspberries
";
$records = db::execute($sql);
foreach ($records as $record)
{
echo '<pre>Foo == ' . $record['FOO'] . '</pre>';
echo '<pre>Bar == ' . $record['BAR'] . '</pre>';
}
hth,
me
[Back to original message]
|