I actually decided not to do this. I have a class for doing MySQL work and I wanted to make my code database independent. Hence, my question about returning arrays from MySQL queries. However, unless you return only one row of data, this turns into a multidimensional array. I decided that the overhead of putting the data into an array, passing it back, and then pulling it back out of the array was too much. I could be exaggerating the amount of overhead since I never tested it.
Now that you have me thinking about it again, I want to solve it just for fun.
There are a couple of ways we can setup the array:
$data = array (
$field1 => array ($row1data1, $row2data2),
$field2 => array ($row1data1, $row2data2)
);
You can put data onto the array as you loop through mysql_fetch_array like this:
//Declare 2D array, before you loop of course
$data = array();
array_push($data, array());
//Add data to array
$data[$field1][]=$row1_data_for_field1;
$data[$field2][]=$row1_data_for_field2;
Or we can do this, which is more like the way the data comes from the mysql functions.
$data = array (
$row1 => array (
$field1 => $data_for_field1,
$field2 => $data_for_field2
),
$row2 => array (
$field1 => $data_for_field1,
$field2 => $data_for_field2
)
);
Put data into this array like this:
$data[][$field1] = $data_for_field1;
Where you see brackets, [], without a variable, this should autoincrement with a numerical index.
Check out the array page here
http://www.php.net/manual/function.array.php
I don't know if any of my ramblings made any sense but feel free to email me if you have any questions.
Chris Saylor
saylor@one.net