使用未定义的常量DB_SERVER - 假设为'DB_SERVER'

时间:2011-11-25 19:12:38

标签: php

我有以下php代码:

$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "photos";

define(DB_SERVER,$db_host);
define(DB_USER,$db_user);
define(DB_PASS,$db_pass);
define(DB_NAME,$db_name);

我正在定义DB_SERVER以及您在上面看到的所有其他内容,但由于某种原因,我收到以下错误:

Notice: Use of undefined constant DB_SERVER - assumed 'DB_SERVER' in /Users/ahoura/Sites/photos/include/config.php on line 34 Notice: Use of undefined constant DB_USER - assumed 'DB_USER' in /Users/ahoura/Sites/photos/include/config.php on line 35 Notice: Use of undefined constant DB_PASS - assumed 'DB_PASS' in /Users/ahoura/Sites/photos/include/config.php on line 36 Notice: Use of undefined constant DB_NAME - assumed 'DB_NAME' in /Users/ahoura/Sites/photos/include/config.php on line 37

我做错了什么!?!?!

3 个答案:

答案 0 :(得分:14)

您需要在define()

中引用新常量的名称
define('DB_SERVER',$db_host);
define('DB_USER',$db_user);
define('DB_PASS',$db_pass);
define('DB_NAME',$db_name);

通知表明PHP实际上将它们视为带引号的字符串,因为它无法找到具有这些名称的现有常量,但这是不可靠的依赖。 define()将字符串作为要定义的常量的名称及其预期值。

答案 1 :(得分:-1)

遇到同样的问题,研究这些对我有用:
1.将您的文件config.php重命名为config123.php之类的非常独特的东西,也可以更改require语句。可能是由于与PHP PEAR内部的config.php文件冲突造成的 2.要求您的文件:
        需要dirname( FILE )。 ' /config.php' ;;
3.如果您永远不需要,请从php.ini include_path设置中删除PEAR。

来源:https://community.apachefriends.org/f/viewtopic.php?p=204776

答案 2 :(得分:-2)

我将直接使用数据库信息进行分类,而不是从不同的文件导入。这可能是我猜测XAMPP 1.6.7最新版本的问题。无论如何你可以试试这个。

<?php

    class MySQLDatabase {

        private $connection;

        function __construct() {
           $this->open_connection();
        }

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

    $database = new MySQLDatabase();
?>