Re: convert query result to array
| From: | Philip Thompson | Date: | Wed, 14 May 2008 15:28:24 +0000 |
| Subject: | Re: convert query result to array | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-274314@lists.php.net to get a copy of this message | ||
On May 13, 2008, at 6:35 AM, Sanjeev N wrote:
fetchAll() works if php version is greater than 5. As Wang suggested, we should write our own function. its easy... i have done so many times for larger database. Store earch rows of the resultset in the array (multidimensional ie. 2). and then work on the array using the loops & conditions $query = "select * from tablename"; $result = mysql_query($query); while($arr = somefunction($result)){ $resultarray[] = $arr; } Hope it works!Here's a way to get a 2-dimensional array of results that you can loop through when needed. Don't know if it's what you need, but it's useful when you have more than 1 row as a result. <?php function fetchObjectsAsArray ($sql) {
$result = mysql_query ($sql);
if (!$obj['count'] = mysql_num_rows ($result)) { return null; }
while ($rows[] = mysql_fetch_array ($result)) { }
foreach ($rows as $array) {
foreach ($array as $field => $value) {
$obj[$field][] = $value;
}
}
return $obj;} ?> Have fun! ~Phil
On 5/13/08, Forcey <forcey@gmail.com> wrote:I guess PDOStatement::fetchAll() should work? see http://www.php.net/manual/en/pdostatement.fetchall.php for details. - Forcey On Tue, May 13, 2008 at 9:55 AM, Yi Wang <wangyi6854@gmail.com> wrote:I think flance's meaning is whether there is a build-in function that can convert the result set to an array. The short answer is: do it yourself. On 5/12/08, Stut <stuttle@gmail.com> wrote:same nameOn 12 May 2008, at 15:56, It flance wrote:is there any function that can convert the result of query to anassociative array?what i want is the following: $query = "select * from tablename"; $result = mysql_query($query); $arr = somefunction($result); where $arr should be an assoiative array whose indices have theas the fields names of table tablename.http://php.net/mysql_fetch_assoc Please please please read the manual: http://php.net/mysql -Stut