Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sqlite PDO query returns no results

Tags:

sql

php

sqlite

pdo

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.

like image 657
Elliot Rocha Avatar asked Oct 15 '25 14:10

Elliot Rocha


1 Answers

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);
?>
like image 96
enterx Avatar answered Oct 18 '25 07:10

enterx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!