Page 1 of 1

PDO help

Posted: Wed Aug 06, 2008 8:50 am
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

Re: PDO help

Posted: Wed Aug 06, 2008 9:26 am
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?