Page 1 of 1

PDO loop problem

Posted: Mon Jun 23, 2014 3:06 am
by barb woolums
Can someone please tell me what's wrong with this:

Code: Select all

$rdb = new PDO("$dbtype:host=$dbhost;dbname=$dbrecipes", $dbuser, $dbpass);
$sql="call query_recipes_with_name_id_owner_visible()";
$dbhidden = $rdb->prepare($sql);
$dbhidden->execute();
$hidden=0;
while($dbhidden->fetchAll()) {
      $hidden++;
}
When I run the procedure in phpmyadmin it returns one row with 4 columns (it could return more rows), but hidden never gets incremented. I tried with fetch instead of fetchall but that didn't work either.

The only reason I am doing this is because rowCount() doesn't work as it's a mysql select.

Re: PDO loop problem

Posted: Mon Jun 23, 2014 4:05 pm
by Christopher
First check that the query actually works. Then PDOStatement::fetchAll() return false on error or and array. So you should first check the result with an if() for an error. Then probably foreach() through the array if no error.

Re: PDO loop problem

Posted: Mon Jun 23, 2014 8:41 pm
by barb woolums
I worked it out. I actually got rowCount() working by closing the cursor after the previous query - yay.

Thanks for your help