我试图在php中创建一个简单的注册系统。我有一个名为Admin(内部控制器文件夹)的类,它扩展了DBConnection.php类。 Admin类有一个注册方法,可以让注册注册但在那里遇到问题。 'include_once'出错,错误显示'警告:include_once(../ Database / DBConnection.php):无法打开流:C:\ xampp \ htdocs \ WoodlandsAway \ controller \ Admin.php中没有此类文件或目录第15行----- “ '警告:include_once():无法在C:\ xampp \ htdocs \ WoodlandsAway \ controller \ Admin.php中打开'../Database/DBConnection.php'以包含(include_path ='C:\ xampp \ php \ PEAR')在第15行----- “ '致命错误:在第17行的C:\ xampp \ htdocs \ WoodlandsAway \ controller \ Admin.php中找不到类'DBConnection'
这是我的include_once代码
include_once ('../Database/DBConnection.php');
And here is my folder structure
- DBConnection.php
class DBConnection {
//put your code here
private $host;
private $user;
private $pass;
private $database;
private $conn;
function DBConnection() {
$this->host = 'localhost';
$this->user = 'root';
$this->pass = '';
$this->database = 'woodlands_away';
}
public function getConnections() {
$this->conn = new mysqli($this->host, $this->user, $this->pass, $this->database) or
die($this->conn->error);
return $this->conn;
}
}
和Admin.php
include_once ('../Database/DBConnection.php');
class Admin extends DBConnection {
public function Admin() {
parent::DBConnection();
}
public function signup($username, $password) {
$sql = "insert into users values(".$username.", ".$password.")";
return $this->getConnections()->query($sql);
}}
答案 0 :(得分:1)
首先,我建议您声明一个表示项目根路径的常量。这个常量必须以一种独特的方式声明,例如index.php或类似的,但在项目的根目录中:
define('PROJECT_ROOT_PATH', __DIR__);
然后你的包含电话应该是这样的:
include_once (PROJECT_ROOT_PATH . '/Database/DBConnection.php');
(始终指定前导斜杠)
问题是目前您的代码可能依赖于Working directory,因此您可能会获得意外的工作目录。