PHP define()似乎没有使用include() [英] PHP define() doesn't seem to be working with include()
问题描述
我一直在OO PHP尝试我的手,目前有三个文件。我有一个class_lib.php,目前,只有一个databaseServer类,一个index.php文件和definitions.php文件。我想把所有敏感的数据库信息放入定义文件。但是,当我这样做,我在尝试连接到数据库时出现错误:Unkown server DB_HOST。我的定义文件为:
<?php
define(DB_HOST,localhost);
define(DB_USER,root);
define(DB_PASS,password);
define(DB_NAME,database);
?>
然后在索引文件中使用它们:
include('definitions.php');
include('class_lib.php');
$ testing = new databaseServer();
$ testing-> connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
我在databaseServer类中使用的函数是:
function connect($ host,$ user,$ pw,$ db){
$ this-> con = mysql_connect $ pw);
if(!$ this-> con){
die('Could not connect:'。mysql_error());
}
$ this-> selectDb($ db);
}
function selectDb($ database){
$ this-> db = mysql_select_db($ database,$ this-> con);
if(!$ this-> db){
echo无法选择数据库:。 mysql_error();
}
}
任何想法为什么这不行?我也试过将定义文件放入class_lib文件中的include中,但仍然无效。
这应该工作正常,我从来没有见过它不工作。
-
100%确保包含的顺序正确(定义需要先加载)
/ li>
-
在definitions.php文件和索引文件中测试输出常量值
- 100%确保您正在拨打正确的档案
I've been trying my hand at OO PHP, and currently have three files. I have a class_lib.php which, at the moment, just has a databaseServer class, an index.php file and a definitions.php file. I want to put all my sensitive database info into the definitions file. However, when I do, I get an error when trying to connect to the database: "Unkown server DB_HOST". My definitions file is:
<?php
define("DB_HOST","localhost");
define("DB_USER","root");
define("DB_PASS","password");
define("DB_NAME","database");
?>
Then I use them in the index file like so:
include('definitions.php');
include('class_lib.php');
$testing = new databaseServer();
$testing->connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
And the function I use in the databaseServer class is this:
function connect($host,$user,$pw,$db) {
$this->con = mysql_connect($host,$user,$pw);
if (!$this->con) {
die('Could not connect: ' . mysql_error());
}
$this->selectDb($db);
}
function selectDb($database) {
$this->db = mysql_select_db($database,$this->con);
if (!$this->db) {
echo "Could not Select database: " . mysql_error();
}
}
Any ideas why this would not work? I've also tried putting the definitions file into an include in the class_lib file, but it still doesn't work.
This should work fine, and I've never seen it not work.
Make 100% sure the includes are in the correct order (the defines need to be loaded first)
Make test outputs of the constant values in the "definitions.php" file and the index file
Make 100% sure you are calling the right files
这篇关于PHP define()似乎没有使用include()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!