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.

4
  • 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. Commented Jul 26, 2016 at 0: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. Commented Jul 26, 2016 at 0:09
  • what about checking for errors php.net/manual/en/pdo.error-handling.php and php.net/manual/en/function.error-reporting.php - anything from that? Commented Jul 26, 2016 at 0: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? Commented Jul 26, 2016 at 0:17

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

7 Comments

Or you simply check if the database file exists / is writeable :)
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, this is expected - it's exactly what your code does.
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?
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.