What does PDO (MYSQL) return when there is an empty result set?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • guillermobytes
    New Member
    • Jan 2010
    • 77

    What does PDO (MYSQL) return when there is an empty result set?

    Hi,

    I was wondering what does PDO return when there are no records matching the query? i.e. i query for a column.name = 'John' and there is no 'John' in the table.


    thanks for your answer.

    guillermobytes
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    It returns an "empty" PDOStatement object, in much the same way the classic mysql_query function returns an "empty" resource.

    Comment

    • guillermobytes
      New Member
      • Jan 2010
      • 77

      #3
      thnaks Atli,

      so how should i test that it is an emptyPDO statement?
      Code:
      $dbh = new PDO('connection info',...);
      $sql = 'SELECT something FROM somewhere WHERE id = inexistent';
      $ps = $dbh->query($sql);
      if ($ps->rowCount() > 0) {
          $resultSet = $ps->fetchAll();
          print_r($resultSet);
      } else {
          echo 'Empty result set';
      }

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        PDOStatement::r owCount() does that for you.

        Comment

        Working...