-2

如果我尝试在浏览器中打开这个简单的文件:

<?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);

?>

这样,它可以正常工作,我不明白为什么文件和路径是正确的,因为在第二段代码中所有工作。

4

3 回答 3

5

您的脚本var/www/classes/Mysql.php已经classes 目录中。您包含的文件位于同一目录中。删除classes/并使用:

require_once 'settings.php';
于 2012-10-18T14:11:59.547 回答
4

由于您已经在 classes 文件夹中,因此您有两个选择

第一种选择是使用相对路径

require_once 'settings.php';

第二种选择是使用绝对路径

require_once $_SERVER['DOCUMENT_ROOT'] . '/classes/settings.php'

于 2012-10-18T14:12:01.690 回答
0

尝试使用$_SERVER['DOCUMENT_ROOT']

require_once ($_SERVER['DOCUMENT_ROOT'] . '/classes/settings.php');
于 2012-10-18T14:14:59.330 回答