开发者

Get the full result

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

开发者_如何学GoSELECT * FROM table

Usually I would make something like this:

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


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


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.


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.


As of PHP 5.3 there is a built in function:

fetch_all

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜