How to show SQL errors in SQLite?

Using SQLite in PHP (using PDO), I have this code:

try {
$db = new PDO("sqlite:C:\Program Files\Spiceworks\db\spiceworks_prod.db");

echo "Done.<br /><b>";

$query = "SELECT id FROM Devices LIMIT 5";

echo "Results: ";

$result = $db->query($query);

while ($row = $result->fetchArray()) {
    print_r($row)."|";
}
}
catch(PDOException $e) {  
    echo $e->getMessage();  
}

But this does not output any data from SQL. I know there is data in the database and the connection is valid. If I change the request, I will say:

$query = "SELECT BLAHid FROM FakeDevices LIMIT 5";

Nothing changes. Nothing of SQL prints again, and I see no errors, although this is clearly an invalid SQL query.

In both situations, Finish and Results are printed in order. How to print SQL errors, for example, if the query is invalid?

+2
source share
1 answer

You need to point the PDO to an exception. You can do this by adding the following line after connecting to the database:

$db = new PDO("sqlite:C:\Program Files\Spiceworks\db\spiceworks_prod.db");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

, , , .

+6

Source: https://habr.com/ru/post/1784277/


All Articles