Getting no results no matter how broad my query PHP: 5.3 Sqlite3: 3.6 PDO: 5.3.3 I would think this should be a very simple process but even looking around I still don’t know why I’m getting 0 results. Here is my code:
<?php $sqlite = new PDO('sqlite:/example.db'); $result = $sqlite->query('SELECT * from foo'); if(!$result) { echo 'fail'; return false; } ?>
Any ideas on what I am doing wrong? The ‘foo’ table will only have four columns, and this test db only has one table. Running the query in sqlite displays the results fine.
Advertisement
Answer
You have to execute the statement first than fetch the result.
You might add try/catch block around the execute method call. and do some error handling.
Here’s an example of catching an Exception. Do not use it as a design guideline.
<?php try { $sqlite = new PDO('sqlite:/example.db'); } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); } $statement = $sqlite->prepare('SELECT * from foo'); try { $statement->execute(); } catch(PDOException $e) { echo "Statement failed: " . $e->getMessage(); return false; } $result = $statement->fetchAll(); var_dump($result); ?>