PDO SQllite error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rohitskapoor
    New Member
    • Sep 2008
    • 1

    PDO SQllite error

    Hi,

    I am trying to run this code on the Linux box...

    Code:
    <?php
    
    // create a SQLite3 database file with PDO and return a database handle (Object Oriented)
    try{
    
    $dbHandle = new PDO('sqlite:test.db');
    
    }catch( PDOException $exception ){
    
    die($exception->getMessage());
    
    }
    // check if table/s needs to be created (PDO)
    $statement = $dbHandle->query('SELECT * FROM trace');
    $result = $statement->fetchAll();
    
    if( sizeof($result) == 0 ){
      echo 'No database table/s found.';
    }
    
    
    ?>
    I am getting the following error....

    Fatal error: Call to a member function fetchAll() on a non-object in /usr/local/apache2/htdocs/zonebee/dev/PDORohit/pdosample2.php on line 15


    Any thoughts?

    Regards,
    Rohit
    Last edited by rohitskapoor; Sep 26 '08, 11:13 AM. Reason: Wrong code..
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Hey there, Rohitskapoor.

    Welcome to the forums.

    Please remember to post using [code] tags. [code] code goes here ... [/code]

    Cheers.

    Moderator

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

      The PDO query function is obviously not returning the statement object as it should. Not sure why it would do that, the documentation doesn't mention what the function returns if the query should fail, so I would have assumed it simply returned and empty statement object.

      In any case, try checking the return value of the query function first, like:
      [code=php]
      $stmt = $pdoHandle->query("...") ;

      if($stmt) {
      // Do your thing.
      }
      else {
      echo "Query returned something weird: <br />";
      var_dump($stmt) ;
      }
      [/code]

      Comment

      Working...