Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch all in assoc array from a prepared statement?

Tags:

I'm trying this code:

    if ($result = $this->mysqli->prepare("SELECT * FROM `mytable` WHERE `rows1`=?"))     {          $result->bind_param("i",$id);         $result->execute();         while ($data = $result->fetch_assoc())         {              $statistic[] = $data;          }          echo "<pre>";         var_dump($statistic);         echo "</pre>";     } 

but it's throwing the following error

[Fri Jun 15 12:13:11 2012] [error] [client 127.0.0.1] PHP Fatal error: Call to undefined method mysqli_stmt::fetch_assoc() in [myfile.php]

And also I've tried:

if ($result = $this->mysqli->prepare("SELECT * FROM `mytable` WHERE `rows1`=?"))     {          $result->bind_param("i",$id);         $rows = $result->execute();         while ($data = $rows->fetch_assoc())         {              $statistic[] = $data;          }          echo "<pre>";         var_dump($statistic);         echo "</pre>";     } 

that makes this:

[Fri Jun 15 12:22:59 2012] [error] [client 127.0.0.1] PHP Fatal error: Call to a member function fetch_assoc() on a non-object in [myfile.php]

What else I can do for getting result or what I doing wrong? I need the assoc array from DB looking like $data[0]["id"] = 1

like image 503
AquaVita Avatar asked Jun 15 '12 10:06

AquaVita


People also ask

How to fetch all associative array in php?

The fetch_all() / mysqli_fetch_all() function fetches all result rows and returns the result-set as an associative array, a numeric array, or both. Note: This function is available only with MySQL Native Driver.

Which method fetches all rows from a result as an array?

To return a single row from a result set as an array or object, call the PDOStatement::fetch method. To return all of the rows from the result set as an array of arrays or objects, call the PDOStatement::fetchAll method.

What returns an associative array that corresponds to the fetch row?

pg_fetch_assoc() returns an associative array that corresponds to the fetched row (records).


2 Answers

In fact you can do this quite easily, you just can't do it with the mysqli_stmt object, you have to extract the underlying mysqli_result, you can do this by simply calling mysqli_stmt::get_result(). Note: this requires the mysqlnd (MySQL Native Driver) extension which may not always be available.

However, the point below about recommending PDO over MySQLi still stands, and this is a prime example of why: the MySQLi userland API makes no sense. It has taken me several years of intermittently working with MySQLi for me to discover the mechanism outlined above. Now, I'll admit that separating the statement and result-set concepts does make sense, but in that case why does a statement have a fetch() method? Food for thought (if you're still sitting on the fence between MySQLi and PDO).

For completeness, here's a code sample based (loosely) on the original code in the question:

// Create a statement $query = "     SELECT *     FROM `mytable`     WHERE `rows1` = ? "; $stmt = $this->mysqli->prepare($query);  // Bind params and execute $stmt->bind_param("i", $id);  // Extract result set and loop rows $result = $stmt->get_result(); while ($data = $result->fetch_assoc()) {     $statistic[] = $data; }  // Proof that it's working echo "<pre>"; var_dump($statistic); echo "</pre>"; 
like image 91
DaveRandom Avatar answered Nov 04 '22 00:11

DaveRandom


You can do:

$stmt = $this->mysqli->prepare("SELECT * FROM `mytable` WHERE `rows1`=?"); $stmt->bind_param("i",$id); $stmt->execute(); $result = $stmt->get_result(); $statistic = $result->fetch_all(MYSQLI_ASSOC); 

$statistic contains all the result in a 2-dimensional array.

*It should be noted that this mysqli_fetch_all() function only works with the mysqlnd package. http://php.net/manual/en/mysqli-result.fetch-all.php

like image 22
manojadams Avatar answered Nov 03 '22 23:11

manojadams