简体   繁体   English

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

[英]relative path SQLite3 connection with PDO not working

I have a Class function in PHP ( GetMenuItems.php ) that connects to the DB and gets the menu items. 我在PHP中有一个Class函数( GetMenuItems.php ),该函数连接到数据库并获取菜单项。 This function is called from the Menu.php and MenuAdmin.php files. Menu.phpMenuAdmin.php文件中调用此函数。

The problem is, it works fine on Menu.php and not in MenuAdmin.php . 问题是,它在Menu.php上工作正常,而在MenuAdmin.php无法正常工作

Folder structure: 资料夹结构:

  • localhost 本地主机
    • FundosDB.db FundosDB.db
    • index.php index.php
    • Menu.php Menu.php
    • Class
      • GetMenuItems.php GetMenuItems.php
    • Admin 管理员
      • Admin.php Admin.php
      • MenuAdmin.php MenuAdmin.php

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

If I change the connection to: $db = new PDO('sqlite:..\\FundosDB.db'); 如果我将连接更改为: $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? 而且,相对路径不应该在建立连接的类文件和DB文件之间,而不是在调用该类中的函数的文件与DB文件之间? I am confused. 我很困惑。

Set a path to your db file using absolute path with $_SERVER['DOCUMENT_ROOT'] , something like: 使用$_SERVER['DOCUMENT_ROOT']绝对路径为您的数据库文件设置路径,类似于:

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

Then use it everywhere: 然后在任何地方使用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM