I want to create a sqlite database with these lines:
<?php
echo "here<br/>";
try {
$db_connection = new PDO('sqlite:file.db');
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
echo "there<br/>";
?>
After that I expect the output
here
there
and the file file.db
to exist in the folder.
But I only see here
and there is no file. So it seems like new
fails without even throwing an exception.
Here is what I checked so far:
What else could be/is the problem?
edit
I also tried this following felipsmartins answer:
$ php -i | grep -i sqlite
/etc/php.d/pdo_sqlite.ini,
/etc/php.d/sqlite3.ini,
PHP Warning: Unknown: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in Unknown on line 0
PDO drivers => sqlite
pdo_sqlite
PDO Driver for SQLite 3.x => enabled
SQLite Library => 3.7.17
sqlite3
SQLite3 support => enabled
SQLite3 module version => 0.7
SQLite Library => 3.7.17
sqlite3.extension_dir => no value => no value
But phpinfo()
does not show any PDO
or sqlite
entries.
答案 0 :(得分:4)
正如PHP documentaion中所指定的那样,PDO需要DSN字符串上的绝对路径名:
要访问磁盘上的数据库,请将绝对路径附加到DSN 前缀。
$pdo = new PDO('sqlite:' . __DIR__ . DIRECTORY_SEPARATOR . 'file.db');
当然,请确保您已启用SQLite支持。看一下快速命令:php -i | grep -i sqlite
答案 1 :(得分:2)
如果您的代码既没有抛出异常也没有错误,这意味着您的PDO驱动程序没有安装,关闭,禁用或其他一些原因不可用。
在您的PDO代码之前添加以下代码以确保安装PDO并且它正在运行始终是一个好习惯:
if (!defined('PDO::ATTR_DRIVER_NAME'))
echo 'PDO driver unavailable';