tags:

views:

32

answers:

3

Is there a PHP function to get the full result with a mysql query in a multidimensional array?

SELECT * FROM table

Usually I would make something like this:

$query = mysql_query = ("SELECT * FROM table");
while ($result = mysql_fetch_array($query){
    echo $result[0];
}
+1  A: 

You can create your own function like mysql_fetch_array_complete() and imagine that it's builtin ;-)

zerkms
+1  A: 

You could always write your own function to do this, but it would often lead to an unnecessary iteration through the result set (once when you call your function, another time when you actually USE the resulting array).

Since you're in php5, you could create a database result class that implements the Iterator interface. Then, you can use your class in foreach () loops and have much of the ease-of-use that you get from an array.

grossvogel
what is the major difference between `while` + `mysql_fetch_array` and `foreach` + `custom class`?
zerkms
With the latter you can use foreach on the result and don't have to fetch row by row first.
Franz
+1  A: 

If you are using PDO to access mysql there is. http://www.php.net/manual/en/pdostatement.fetchall.php

Otherwise you need to do it yourself.

$query = mysql_query = ("SELECT * FROM table");
$all_results = array();
while ($result = mysql_fetch_array($query){
    $all_results[] = $result;
}
print_r($all_results);

The $all_results variable will be a multi-dimensional array with all the records.

Brent Baisley
If he were using PDO i bet he would use PDO methods, not `mysql_*` ;-)
zerkms