2

I use XAMPP with

  • PHP Version 5.4.7
  • PDO drivers - mysql, sqlite - enabled
  • SQLite Library - 3.7.7.1 - enabled
  • sqlite3 - SQLite3 module version - 0.7 - enabled
  • sqlite3.extension_dir - no value - no value
  • API Extensions - mysql,mysqli,pdo_mysql

So, if I understood well some tutorials, that's all I need to be able to start working.

So, in C:\xampp\htdocs\my_project I copied database.db file (sakila downloaded from here: https://github.com/joshuakleveter/sakila_db/tree/master/public_html )

Then I created index.php that looks like this:

<?php

try {
    $db = new PDO('sqlite:./database.db');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(Exception $e) {
    echo 'Error: ';
    echo $e->getMessage();
    die();
}

and when I run this there are no errors. As you can see, my path is /database.db. If I change it to just

$db = new PDO('sqlite:.database.db');

no errors. Or without dot:

$db = new PDO('sqlite:database.db');

or absolute path - there are no errors.

Even worse, if I put wrong not existing .db name:

$db = new PDO('sqlite:daaaaaaaaaaaatabase.db');

also - there are no errors.

Can you tell me what am I doing wrong?

With all of the above paths I've tried the following:

$results = $db->query('select * from film');
var_dump($results);
die();

and I get "General error: 1 no such table", but the database.db has that table (I installed sqlite, opened that database in command line and checked - the database.db is OK).

1
  • 1
    why should it error? If you provide a "non-existent" db, pdo will CREATE that db file for you. the only time it should fail is if it can't create the file (e.g. wrong path, invalid permissions). Commented Sep 25, 2015 at 16:14

1 Answer 1

2

Connecting to a not yet existing file is how new databases are created.

Relative paths are relative to the current directory, which you usually cannot control, except by setting it to some absoulte path. Just use an absolute path directly when opening the DB.

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

1 Comment

I already wrote that I also tried it with absolute path and in that case i also get error for: $results = $db->query('select * from film'); "General error: 1 no such table", but the database.db has that table (I installed sqlite, opened that database in command line and checked - the database.db is OK).

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.