1

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.

KPM
  • 181
  • 4
  • 23
  • I've a feeling your try/catch isn't kicking in. What happens when you remove `echo "Database loaded successfully ....";`? and where is this defined $error ? error reporting should have thrown you something about it. – Funk Forty Niner Jul 26 '16 at 00:06
  • When I remove the echo line I get "Handle has been created......" If I change the name of the db to something else, I still get the same success message. – KPM Jul 26 '16 at 00:09
  • what about checking for errors http://php.net/manual/en/pdo.error-handling.php and http://php.net/manual/en/function.error-reporting.php - anything from that? – Funk Forty Niner Jul 26 '16 at 00:10
  • Nothing.... enabled all error levels and the code is still reporting success. I'm wondering is it creating a new db each time it runs? – KPM Jul 26 '16 at 00:17

1 Answers1

2

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:/");)

Dekel
  • 60,707
  • 10
  • 101
  • 129
  • Or you simply check if the database file exists / is writeable :) – icecub Jul 26 '16 at 00:19
  • I tried the code above and I receive the error message SQLSTATE[HY000] [14] unable to open database file Database -- NOT -- loaded successfully .. Query Closed !!! PDOException: SQLSTATE[HY000] [14] unable to open database file in C:\Users\max\Desktop\Graspe\graspe\db_connect.php:13 Stack trace: #0 C:\Users\max\Desktop\Graspe\graspe\db_connect.php(13): PDO->__construct('sqlite:/') #1 {main} – KPM Jul 26 '16 at 00:20
  • @KPM, this is expected - it's exactly what your code does. – Dekel Jul 26 '16 at 00:23
  • So from that message, I'm assuming it is just creating a new db each time it runs and isn't actually connecting to the db that I have set up in PHPmyadmin on the localhost. Any ideas? – KPM Jul 26 '16 at 00:23
  • Yes..looking back at the control panel, I have mixed the two of them up. Thanks for the info. I'll try using MySQL commands to connect. – KPM Jul 26 '16 at 00:26
  • It was a MySQL db.... I changed the code to the following and it works successfully – KPM Jul 26 '16 at 00:32
  • Thanks for the help – KPM Jul 26 '16 at 00:33