0

我有一个数据库类,我在其中使用 required_once 调用配置文件。我已经使用define() 声明了一些常量。我收到未定义常量的错误,并且还通过引用分配了 new 的返回值。这是配置文件代码:

<?php

// Database Constants
defined("DB_SERVER") ? null : define("DB_SERVER", "localhost");
defined("DB_USER")   ? null : define("DB_USER", "root");
defined("DB_PASS")   ? null : define("DB_PASS", "");
defined("DB_NAME")   ? null : define("DB_NAME", "shareysmile");

?>

这是我的数据库类代码(两者都在同一个目录中,所以这不是问题):

<?php
require_once("config.php");

class MySQLDatabase {

    private $connection;
    public $last_query;
    private $magic_quotes_active;
    private $real_escape_string_exists;

  function __construct() {
    $this->open_connection();
        $this->magic_quotes_active = get_magic_quotes_gpc();
        $this->real_escape_string_exists = function_exists( "mysql_real_escape_string" );
  }

    public function open_connection() {
        $this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
        if (!$this->connection) {
            die("Database connection failed: " . mysql_error());
        } else {
            $db_select = mysql_select_db(DB_NAME, $this->connection);
            if (!$db_select) {
                die("Database selection failed: " . mysql_error());
            }
        }
    }

    public function close_connection() {
        if(isset($this->connection)) {
            mysql_close($this->connection);
            unset($this->connection);
        }
    }

    public function query($sql) {
        $this->last_query = $sql;
        $result = mysql_query($sql, $this->connection);
        $this->confirm_query($result);
        return $result;
    }

    public function escape_value( $value ) {
        if( $this->real_escape_string_exists ) { // PHP v4.3.0 or higher
            // undo any magic quote effects so mysql_real_escape_string can do the work
            if( $this->magic_quotes_active ) { $value = stripslashes( $value ); }
            $value = mysql_real_escape_string( $value );
        } else { // before PHP v4.3.0
            // if magic quotes aren't already on then add slashes manually
            if( !$this->magic_quotes_active ) { $value = addslashes( $value ); }
            // if magic quotes are active, then the slashes already exist
        }
        return $value;
    }

    // "database-neutral" methods
  public function fetch_array($result_set) {
    return mysql_fetch_array($result_set);
  }

  public function num_rows($result_set) {
   return mysql_num_rows($result_set);
  }

  public function insert_id() {
    // get the last id inserted over the current db connection
    return mysql_insert_id($this->connection);
  }

  public function affected_rows() {
    return mysql_affected_rows($this->connection);
  }

    private function confirm_query($result) {
        if (!$result) {
        $output = "Database query failed: " . mysql_error() . "<br /><br />";
        //$output .= "Last SQL query: " . $this->last_query;
        die( $output );
        }
    }

}

$database = new MySQLDatabase();


?>

我无法理解这个问题,有人可以帮忙吗?以下是我得到的确切错误:

Deprecated: Assigning the return value of new by reference is deprecated in D:\xampp\php\PEAR\Config.php on line 80

Deprecated: Assigning the return value of new by reference is deprecated in D:\xampp\php\PEAR\Config.php on line 166

Notice: Use of undefined constant DB_SERVER - assumed 'DB_SERVER' in D:\xampp\htdocs\sharesmile\src\database.php on line 18

Notice: Use of undefined constant DB_USER - assumed 'DB_USER' in D:\xampp\htdocs\sharesmile\src\database.php on line 18

Notice: Use of undefined constant DB_PASS - assumed 'DB_PASS' in D:\xampp\htdocs\sharesmile\src\database.php on line 18
4

2 回答 2

5

看来config.php您包含的是 include D:\xampp\php\PEAR\Config.php,而不是您的配置文件。这是因为PEAR目录的优先级高于.包含路径。使用绝对路径 ( __DIR__ . '/config.php') 使其工作可靠,或更改包含路径。

于 2012-08-23T11:13:54.757 回答
0

简单地改变

require_once("config.php");

require_once("../includes/config.php");

在你的database.php文件中。

我假设这两个文件都在这里的包含目录中。

于 2014-11-12T19:12:36.523 回答