如何在db文件中调用常量?

时间:2011-10-19 09:30:32

标签: php mysql

这是我的db文件

config.php

define(HOST_NAME, "localhost");
define(USER, "root");
define(PASSWORD, "");
define(DATABASE, "xxx");

现在我想在我的db文件中调用这些常量

db.php
class Database {
function db_connect() {
$conn =mysql_connect(HOST_NAME, USER, PASSWORD);
}
}
$obj_db = new Database();
$obj_db->db_connect();

但是这些常量不会出现在mysql_connect中,而是给出错误

 Unknown MySQL server host 'HOST_NAME' (11001)

请帮忙 感谢

2 个答案:

答案 0 :(得分:2)

您应config.php包含include 'config.php';

有4种方法可以做到这一点:

include 'config.php';
include_once 'config.php';
require 'config.php';
require_once 'config.php';

PS:定义常量时需要引号。

define('HOST_NAME', "localhost");
define('USER', "root");
define('PASSWORD', "");
define('DATABASE', "xxx");

答案 1 :(得分:1)

使用

include 'config.php';

require 'config.php';