当数据库位于不同文件夹中时,PDO SQLite3 连接失败
我正在尝试从 php 连接到 sqlite3 数据库,但是当我想将数据库放置在方便且位于网络可访问空间之外的位置时,我遇到了困难。当 db 文件与我的 php 脚本位于同一文件夹中时,它运行正常,但是当我将它放在其他地方时 - 无声失败。
我编写了一个简单的检查器,因此更容易理解我的意思
<?php
$files = array(
'data.db',
getcwd() . DIRECTORY_SEPARATOR . 'data.db',
'some_inner_folder' . DIRECTORY_SEPARATOR . 'data.db',
getcwd() . DIRECTORY_SEPARATOR . 'some_inner_folder' . DIRECTORY_SEPARATOR . 'data.db'
);
foreach($files as $file) {
if(file_exists($file))
echo $file, ' found<br/>';
else
echo $file, ' not found!<br/>';
try {
$db = new PDO('sqlite:host=' . $file);
if($db)
echo 'connection to ', $file, ' made succesfully<br/>';
} catch (PDOException $error) {
echo 'error connecting to ', $file, ' error message: ', $error->getMessage(), '<br/>';
}
$db = null;
}
输出结果是:
data.db found
connection to data.db made succesfully
C:\Web\data.db found
error connecting to C:\Web\data.db error message: SQLSTATE[HY000] [14] unable to open database file
some_inner_folder\data.db found
error connecting to some_inner_folder\data.db error message: SQLSTATE[HY000] [14] unable to open database file
C:\Web\some_inner_folder\data.db found
error connecting to C:\Web\some_inner_folder\data.db error message: SQLSTATE[HY000] [14] unable to open database file
C:\Web\data.db 和 C:\Web\some_inner_folder\data.db 具有相同的内容(文件复制和;粘贴)
PHP 版本 5.3.6,Windows 7 x64
PDO 驱动程序 mysql、sqlite、sqlite2
SQLite 库 3.7.4
我真的不明白为什么它不起作用。
解决的问题
$db = new PDO('sqlite:host=' . $file);
应该是这样的:
$db = new PDO('sqlite:' . $file);
结果 pdo_sqlite 不需要 'host='
I'm trying to connect to a sqlite3 database from php, but I'm hitting a brick wall when I want to place my db somewhere convenient and outside of web accesible space. When db file is in the same folder as my php script it runs ok, but when I place it somewhere else - silent fail.
I've written up a simple checker so it would be easier to understand what I mean
<?php
$files = array(
'data.db',
getcwd() . DIRECTORY_SEPARATOR . 'data.db',
'some_inner_folder' . DIRECTORY_SEPARATOR . 'data.db',
getcwd() . DIRECTORY_SEPARATOR . 'some_inner_folder' . DIRECTORY_SEPARATOR . 'data.db'
);
foreach($files as $file) {
if(file_exists($file))
echo $file, ' found<br/>';
else
echo $file, ' not found!<br/>';
try {
$db = new PDO('sqlite:host=' . $file);
if($db)
echo 'connection to ', $file, ' made succesfully<br/>';
} catch (PDOException $error) {
echo 'error connecting to ', $file, ' error message: ', $error->getMessage(), '<br/>';
}
$db = null;
}
Output turned out to be:
data.db found
connection to data.db made succesfully
C:\Web\data.db found
error connecting to C:\Web\data.db error message: SQLSTATE[HY000] [14] unable to open database file
some_inner_folder\data.db found
error connecting to some_inner_folder\data.db error message: SQLSTATE[HY000] [14] unable to open database file
C:\Web\some_inner_folder\data.db found
error connecting to C:\Web\some_inner_folder\data.db error message: SQLSTATE[HY000] [14] unable to open database file
C:\Web\data.db and C:\Web\some_inner_folder\data.db has the same contents (file copy&paste)
PHP Version 5.3.6, Windows 7 x64
PDO drivers mysql, sqlite, sqlite2
SQLite Library 3.7.4
I'm not really seeing why it doesn't work.
Problem solved
$db = new PDO('sqlite:host=' . $file);
should read like this:
$db = new PDO('sqlite:' . $file);
Turns out pdo_sqlite doesn't need 'host='
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我们为 Drupal 7 编写数据库层时,我们发现 PDO 有很多错误模式,只有例外才值得一提。
$driver_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
这将使静默故障变得更加嘈杂。When we were coding the database layer for Drupal 7 we found that PDO have many error modes and only exception is worth a damn.
$driver_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
this will make the silent failure a lot more noisy.您对其他文件夹是否有权限问题。它必须能够创建和写入文件(即您必须能够写入目录和文件)。
Do you have a permissions problem on the other folder. It must be capable of creating and writing the file (ie you must be able to write to the directory and the file).