PDO help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AntonioCS
Forum Newbie
Posts: 11
Joined: Tue Jan 15, 2008 12:31 pm

PDO help

Post by AntonioCS »

I am using pdo in my db class abstraction and I try to use a method to see if the result set has some values.
The problem is rowCount (pdo method to return the count) will always return the number of items affected by the query, even if I have used fetch. So does anyone know how I can get the correct number?

Small example of what I mean

Code: Select all

 
try {
    $pdo = new PDO("mysql:host=localhost;dbname=db","pass","username");
  } catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
  }
  
  $sql = "SELECT * FROM teste"; //this will return 4 rows
  
  $res = $pdo->query($sql);
  
 
 
  while (($row = $res->fetch(PDO::FETCH_OBJ))) {
        echo $res->rowCount();  // <-- This will always echo 4
        echo $row->texto . "<br>";                 
  }
 
 
I know that the manual tells us not to use the rowcount in case of a select, because It might not return the correct row count. But in this case it's returning the correct row count and I just want to know of a way to count the items in the row set
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

Re: PDO help

Post by ghurtado »

I don't understand your question. If the query returns 4 rows, but that is not the number you want, what is the number you are looking for?
Post Reply