0

I have PHP 5.4.16 running on my Centos7 server with SQLite3 3.7.17. I am able to use python to write to the database and create the database, but when I try to use PHP to read it in on my website I cannot do anything with SQLite3.

<?php

$db = new SQLite3('var/www/html/WebDatabase/AccountDevices.db');

$result = $db->query('SELECT * FROM log');
var_dump($result->fetchArray());
?>

As you can see my code is basic so I don't think that is the issue, it's just php isn't playing nice with SQLite3.

My current php.ini file:

[sqlite]
http://php.net/sqlite.assoc-case
;sqlite.assoc_case = 0

[sqlite3]
sqlite3.extension_dir=/usr/bin/sqlite3

I was getting an error that was in the ballpark of SQLite is not installed, then I installed php-pdo but was unable to get that to work either. php -m

[PHP Modules]
bz2
calendar
Core
ctype
curl
date
ereg
exif
fileinfo
filter
ftp
gettext
gmp
hash
iconv
json
libxml
mhash
mysql
mysqli
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
readline
Reflection
session
shmop
SimpleXML
sockets
SPL
sqlite3
standard
tokenizer
xml
zip
zlib

[Zend Modules]

So SQLite3 is on and so is that PDO thing(I don't know what this is.)

What am I messing up here? I have been at it for about 4 hours trying a bunch of stuff to work this out. I have gotten to this error now.

PHP Fatal error:  Uncaught exception 'PDOException' with message 'invalid data source name' in /var/www/html/accounts.php:3
Stack trace:
#0 /var/www/html/accounts.php(3): PDO->__construct('var/www/html/We...')
#1 {main}
  thrown in /var/www/html/accounts.php on line 3

Wot do?

Edit:

PHP Fatal error:  Uncaught exception 'PDOException' with message 'invalid data source name' in /var/www/html/accounts.php:3
Stack trace:
#0 /var/www/html/accounts.php(3): PDO->__construct('var/www/html/We...')
#1 {main}
  thrown in /var/www/html/accounts.php on line 3

error when $db = new PDO('var/www/html/WebDatabase/AccountDevices.db'); is being used.

1 Answer 1

1

PHP Fatal error: Uncaught exception 'PDOException' with message 'invalid data source name' in /var/www/html/accounts.php:3

If you're using PDO, then you don't just create the handle with the file name, you have to provide a properly formatted DSN string as noted here.

Something like:

$db = new PDO('sqlite:var/www/html/WebDatabase/AccountDevices.db');

And note that you probably want a fully qualified path instead of a relative one:

$db = new PDO('sqlite:/var/www/html/WebDatabase/AccountDevices.db');

See here for some good examples of PDO/SQLite.

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

5 Comments

I was doing that too, but that was giving the same error as using the sqlite3.
It couldn't have given the same error, PDOException can't be thrown by Sqlite3.
Can I just not use the PDO thing? I don't understand why I need this and why PHP just cannot called sqlite3.
PHP has two different ways to access SQLite databases. You can either use the native SQLite3 driver directly or the PDO abstraction layer with a SQLite DSN. When starting a new project, most people would tend to choose PDO because it allows you to change databases later without changing any code. If you know for sure that you'll always use SQLite, the native driver will likely be a bit simpler to use.
Ya I don't quite know what happened, but today I did the PDO the same way I did it on Friday and it just worked. I have no clue what happened.

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.