当包含来自不同目录时,sqlite创建2个数据库

So i got a class like this:

class Db {
    protected static $dbh = false;

    function connect(){
        try {
        self::$dbh = new PDO("sqlite:./test.db");
        self::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        self::$dbh->exec("CREATE TABLE IF NOT EXISTS news (id INTEGER PRIMARY KEY AUTOINCREMENT, tytul TEXT, tresc TEXT, data DATE, wazne TINYINT(1))");
        } catch (PDOException $e) {
            echo $e->getMessage();
        }

    }
}

There is more to it like fetch functions etc. I have this class stored like this:

|otherfile.php
|admin
  |inc/db.inc
  |somefile.php

So my problem is when i require('db.inc'); from within somefile.php and use the class it creates a new test.db in folder "admin". But when i require it in file "otherfile.php" and use the class it creates a seperate test.db file in the same folder as "otherfile.php". How can i force it to use one file, while still using a relative path to the db?

edit: Personally i think this would work the best:

function connect(){
    try {
    $dir = __DIR__.'/test.db';
    self::$dbh = new PDO("sqlite:".$dir);
    self::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    self::$dbh->exec("CREATE TABLE IF NOT EXISTS news (id INTEGER PRIMARY KEY AUTOINCREMENT, tytul TEXT, tresc TEXT, data DATE, wazne TINYINT(1))");
    } catch (PDOException $e) {
        echo $e->getMessage();
    }

}

This does work locally with my xampp setup, but doesn't work on a remote server sadly.

edit: Finally figured it out, see answer.

Thanks for the answers anyway :-)

dtsjq28482
dtsjq28482 这是不可能的,因为您将从不同的位置访问该类,并且相对路径将不起作用。
8 年多之前 回复
dongqu9917
dongqu9917 是的,这会工作,我知道它,我只是问,如果仍然使用相对路径是否可能
8 年多之前 回复
doufan1899
doufan1899 尝试在引用数据库时使用数据库类的完整路径,而不是./
8 年多之前 回复

2个回答

You can pass a path to your connection function

function connect($path_to_sql_db) {
  try {
    self::$dbh = new PDO("sqlite:"+$path_to_sql_db);
    self::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    self::$dbh->exec("CREATE TABLE IF NOT EXISTS news (id INTEGER PRIMARY KEY AUTOINCREMENT, tytul TEXT, tresc TEXT, data DATE, wazne TINYINT(1))");
    } catch (PDOException $e) {
        echo $e->getMessage();
    }


}

Since your files are in different places the same relative path will not work, as you are observing.

dtc99987
dtc99987 我把答案编辑成了我的问题,因为我无法自己回答。
8 年多之前 回复

Finally figured it out, my local php setup has php > version 5.3, the version on the remote server was below 5.3 so __DIR__ wasn't avaiable and i had to use dirname(__FILE__) instead, and it finally worked like intended:

function connect(){
    try {
    $dir = dirname(__FILE__).'/test.db';
    self::$dbh = new PDO("sqlite:".$dir);
    self::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    self::$dbh->exec("CREATE TABLE IF NOT EXISTS news (id INTEGER PRIMARY KEY AUTOINCREMENT, tytul TEXT, tresc TEXT, data DATE, wazne TINYINT(1))");
    } catch (PDOException $e) {
        echo $e->getMessage();
    }

}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐