1

I created a database with one table using SQLite. However, I am trying to get the data using PHP code but it doesn't work. Any help?

My code:

if ($pdo == null) {
    $pdo = new PDO('sqlite:/db/attendance');
}

$result = $myPDO->query("SELECT * from Student");

enter image description here

4
  • You don't say anything about the version of PHP you're using, but it might not support it. Read up here: php.net/manual/en/ref.pdo-sqlite.php Commented Feb 26, 2017 at 3:48
  • I am using php 5.6, the SQLiteconnection connects successfully to the DB but I can run it from the php code Commented Feb 26, 2017 at 3:51
  • 1
    What does a var_dump($result) give you? Commented Feb 26, 2017 at 3:57
  • Actually, there is not result as the line gives an error: Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [14] unable to open database file' in /Attendance/index.php:16 Stack trace: #0 /Attendance/index.php(16): PDO->__construct('sqlite:/db/atte...') #1 {main} thrown in /Attendance/index.php on line 16 Commented Feb 26, 2017 at 4:00

1 Answer 1

1

When you ask PHP to open /db/attendance you are specifying an absolute path: a file called attendance in a directory called db in the root of your filesystem.

You probably want to specify either a relative path, or an absolute path that is built from your script's location.

Try leaving the leading / off of the front of your path:

$pdo = new PDO('sqlite:db/attendance');

or, even better, building it from the __DIR__ magic constant:

$pdo = new PDO('sqlite:' . __DIR__ . '/db/attendance');
Sign up to request clarification or add additional context in comments.

Comments

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.