开发者

PDO SQLite3 connection failure when database is in different folder

开发者 https://www.devze.com 2023-03-04 04:46 出处:网络
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 fol

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='


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消