0

What I am trying to do is get a product using:

$query = "CALL GetAllProducts()";
$statement = $this->connection->prepare($query);
$statement->execute();
return $statement;

and while I am looping through the results like this:

while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
    extract($row);
    ....
}

I am also querying three different one-to-many relationships in the above while loop. I am getting a list of product variants, product images, and another one....

Here is an example of how I get one of the on-to-many data:

$query = "CALL GetProductImages(" . $id . ")";
$statement = $this->connection->prepare($query);
$statement->execute();

The issue I am having is I am getting this error:

Fatal error: Uncaught PDOException: SQLSTATE[HY000 ]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement: :fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO: :MYSQL_ATTR_USE_BUFFERED_QUERY attribute.

I now want to use PDOStatement::fetchAll() to get all the one-to-many data to avoid the above error but I can't find a solution on how to use it.

Thanks in advance!

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Think_Twice
  • 229
  • 4
  • 18
  • Why not, its documented in the manual – RiggsFolly Sep 09 '20 at 11:10
  • @RiggsFolly where, I have been looking for it. I would like to know how do I get the data from it – Think_Twice Sep 09 '20 at 11:11
  • 2
    **Big Point** There is no security against [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) when you prepare a query that you have already concatentated a value into – RiggsFolly Sep 09 '20 at 11:12
  • 1
    The [PHP Manual page for fetchAll()](https://www.php.net/manual/en/pdostatement.fetchall) found easily and quickly using google – RiggsFolly Sep 09 '20 at 11:13
  • 1
    **Also** `extract($row);` is a horrible and potentially dangerous command as well, and there is almost never a good reason for using it specially not when run in the global scope – RiggsFolly Sep 09 '20 at 11:16
  • It returns an array of rows. The rows are either returned as arrays (so an array of arrays) or as objects (so an array of objects) – RiggsFolly Sep 09 '20 at 11:20
  • fetchAll() won't help you here. – Your Common Sense Sep 09 '20 at 11:34

0 Answers0