相对路径SQLite3与PDO的连接不起作用

相对路径SQLite3与PDO的连接不起作用

问题描述:

I have a Class function in PHP (GetMenuItems.php) that connects to the DB and gets the menu items. This function is called from the Menu.php and MenuAdmin.php files.

The problem is, it works fine on Menu.php and not in MenuAdmin.php.

Folder structure:

  • localhost
    • FundosDB.db
    • index.php
    • Menu.php
    • Class
      • GetMenuItems.php
    • Admin
      • Admin.php
      • MenuAdmin.php

Connection: $db = new PDO('sqlite:FundosDB.db');

If I change the connection to: $db = new PDO('sqlite:..\FundosDB.db'); the opposite happens.

How can I make it work for both situations? And, the relative path shouldn't be between the class file where the connection is made and the DB file instead of between the file that calls the function in the class and the DB file? I am confused.

我在PHP中有一个Class函数( GetMenuItems.php strong>),它连接到DB 并获取菜单项。 此功能是从 Menu.php strong>和 MenuAdmin.php strong>文件中调用的。 p>

问题是,它适用于 Menu.php strong>,而不是 MenuAdmin.php strong>。 p> \ n

文件夹结构: p>

  • localhost
    • FundosDB.db li>
    • index。 php li>
    • Menu.php li>
    • Class
      • GetMenuItems.php li> ul> LI>
      • 管理
        • admin.php的 LI>
        • MenuAdmin.php LI> UL> LI> \ n ul> li> ul>

          连接: strong> $ db = new PDO('sqlite:FundosDB.db') ; code> p>

          如果我将连接更改为: $ db = new PDO('sqlite:.. \ FundosDB.db'); code>相反 发生。 p>

          如何让它适用于这两种情况? 并且,相对路径不应该在进行连接的类文件和DB文件之间,而不是在调用类中的函数的文件和DB文件之间? 我很困惑。 p> div>

Set a path to your db file using absolute path with $_SERVER['DOCUMENT_ROOT'], something like:

define('DB_PATH', $_SERVER['DOCUMENT_ROOT'] . '/FundosDB.db');

Then use it everywhere:

$db = new PDO('sqlite:' . DB_PATH);