php define __file__,PHP define()似乎沒有使用include()

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:

我一直在嘗試使用OO PHP,目前有三個文件。我有一個class_lib.php,目前只有一個databaseServer類,一個index.php文件和一個definitions.php文件。我想將所有敏感數據庫信息放入定義文件中。但是,當我這樣做時,我在嘗試連接數據庫時遇到錯誤:“Unkown server DB_HOST”。我的定義文件是:

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:

我在databaseServer類中使用的函數是這樣的:

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.

任何想法為什么這不起作用?我也嘗試將定義文件放入class_lib文件中的include中,但它仍然不起作用。

6 个解决方案

#1

3

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)

100%確保包含的順序正確(需要先加載定義)

Make test outputs of the constant values in the "definitions.php" file and the index file

在“definitions.php”文件和索引文件中生成常量值的測試輸出

Make 100% sure you are calling the right files

100%確定您正在調用正確的文件

#2

2

Please check if your server supports short_open_tag this value would be commented. I just enabled it with on and it started working.

請檢查您的服務器是否支持short_open_tag,這個值會被評論。我剛啟用它,它開始工作。

#3

0

call it config.php

叫它config.php

if(!$link)

{

die('Failed to connect to server: ' . mysql_error());

}

$db = mysql_select_db(DB_DATABASE);

if(!$db)

{

die("Unable to select database");

}

?>

then include("config.php"); on your pages

然后包括(“config.php”);在你的網頁上

#4

0

working

加工

define('KLINGON_SEPARATOR', '');

?>

not working

不工作

define('KLINGON_SEPARATOR', '');

silly... IDEA says "redundant closing tag"

傻... IDEA說“多余的關閉標簽”

#5

-1

You just need to take out the define functions of the destinations file and transfer it to the source file (in your case "translation.php") and ready to work!

您只需要取出目標文件的define函數並將其傳輸到源文件(在您的情況下為“translation.php”)並准備工作!

Like this the bug with define functions on receiving string params do not happen and you will include the CONSTANTS implemented already.

像這樣,接收字符串參數的定義函數的錯誤不會發生,你將包括已經實現的CONSTANTS。

I faced this trouble and found myself just this one solution.

我遇到了這個麻煩,發現自己就是這個解決方案。

#6

-1

The key in this is using the constant() function to pull the value from the defined constants:

其中的關鍵是使用constant()函數從定義的常量中提取值:

echo constant("DB_USERNAME");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值