KPM KPM - 4 months ago 20
PHP Question

Connect to localhost sqlite db with php

I have UwAmp installed and running. I have set up a mysqlite db on the localhost and I'm trying to connect to it using the following PHP code:

<?php
try
{
/*** connect to SQLite database ***/
$dbh = new PDO("sqlite:graspe.sqlite");
echo "Handle has been created ...... <br><br>";

}
catch(PDOException $e)
{
echo $e->getMessage();
echo "<br><br>Database -- NOT -- loaded successfully .. ";
die( "<br><br>Query Closed !!! $error");
}
echo "Database loaded successfully ....";
?>


The db is called graspe and when I run this script it says that it has connected successfully. If I change the name of the database to something else it still returns a successfully connected message. What am I doing wrong? Thanks in advance.

Answer

By default when you construct a new connection to sqlite database - that database will be created if it doesn't exists.

If you want to test your code to make sure it throws an exception when the database cannot be created you can try to write to filename that you don't have permissions to (new PDO("sqlite:/");)