PDO loop problem

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

PDO loop problem

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PDO loop problem

Post 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.
(#10850)
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: PDO loop problem

Post 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
Post Reply