Jump to content

PDO help


Acs

Recommended Posts

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

 

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>";                 
  }

[code]

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

[/code]

Link to comment
https://forums.phpfreaks.com/topic/118424-pdo-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.