如果我尝试在浏览器中打开这个简单文件:
<?php
require_once 'classes/settings.php';
class Mysql {
private $conn;
function __construct() {
$this->conn = new mysqli (DB_SERVER,DB_USER,DB_PASSWORD,DB_MEMBER) or die('There was a problem connecting to the database.');
if ($this->conn->connect_errno) {
echo "Failed to connect to MySQL: (" . $this->conn->connect_errno . ") " . $this->conn->connect_error;
}
//echo $mysqli->host_info . "\n";
}
function verify_Username_and_Pass($un, $pwd) {
$query = "SELECT *
FROM users
WHERE username = ? AND password = ?
LIMIT 1";
if($stmt = $this->conn->prepare($query)) {
$stmt->bind_param('ss',$un,$pwd);
$stmt->execute();
if($stmt->fetch()) {
$stmt->close();
return true;
}
}
}
}
?>
我收到此警告错误:
Warning: require_once(classes/settings.php): failed to open stream: No such file or
directory in /var/www/classes/Mysql.php on line 3 Fatal error: require_once(): Failed
opening required 'classes/settings.php' (include_path='.:/usr/share/php:/usr/share/pear')
in /var/www/classes/Mysql.php on line 3
我无法理解为什么,文件setting.php
在该文件夹中,那么问题是什么?
修改:
如果我用另一个文件执行相同操作,例如:
<?php
require_once 'classes/settings.php';
$host = "localhost";
$user = "root";
$pass = "pass";
$databaseName = "membership";
$tableName = "users";
//--------------------------------------------------------------------------
// 1) Connect to mysql database
//--------------------------------------------------------------------------
$con = mysql_connect($host,$user,$pass);
$dbs = mysql_select_db($databaseName, $con);
//--------------------------------------------------------------------------
// 2) Query database for data
//--------------------------------------------------------------------------
$result = mysql_query("SELECT * FROM $tableName"); //query
$array = mysql_fetch_row($result); //fetch result
//--------------------------------------------------------------------------
// 3) echo result as json
//--------------------------------------------------------------------------
echo json_encode($array);
?>
通过这种方式,它的工作没有问题,我无法理解为什么文件和路径是正确的,因为在这第二部分代码中都可以工作。
答案 0 :(得分:5)
您的脚本var/www/classes/Mysql.php
已经在的classes目录中。您包含的文件位于同一目录中。删除classes/
并使用:
require_once 'settings.php';
答案 1 :(得分:4)
由于您已经在classes文件夹中,因此您有两个选项
第一个选项是使用相对路径
require_once 'settings.php';
第二个选项是使用绝对路径
require_once $_SERVER['DOCUMENT_ROOT'] . '/classes/settings.php'
答案 2 :(得分:0)
尝试使用$_SERVER['DOCUMENT_ROOT']
require_once ($_SERVER['DOCUMENT_ROOT'] . '/classes/settings.php');