2011-01-19 64 views
0

我正在嘗試創建一個包含數據庫詳細信息的數據庫配置文件,然後將文件包含在我將執行查詢的文件中,但是我不斷收到錯誤,它似乎正在採用常量一個字符串,而不是它的值。包括PHP常量

這裏是config.php文件代碼:

define("DB", "db", true); 
define("LOGIN", "login", true); 
define("PASS", "pass", true); 
define("HOST", "server.com", true); 

這裏是我include和使用常量:

include("config.php"); 

$province = $_POST['province']; 
$city = $_POST['city']; 
$name_surname = $_POST['name_surname']; 
$email_address = $_POST['email_address']; 
$date = date("m.d.y"); 


$connect = mysql_connect(HOST,LOGIN,PASS) or die(mysql_error()); 
mysql_select_db(DB) or die(mysql_error()); 

mysql_query("INSERT INTO table_name (id, province, city, name_surname, email_address, date) 
VALUES ('NULL', '$id', '$province', '$city', '$name_surname', '$email_address', '$date')", $connect) or die(mysql_error()); 

編輯*這是我得到Access denied for user 'USER'@'100-200-0-200.dynamic.adsl.com' (using password: YES)

錯誤

任何想法我哪裏錯了?它可能正在凝視我,但我看不到它。

Thanx提前!

+0

你能發佈錯誤你得到。 – 2011-01-19 14:37:06

+0

你有錯誤嗎? – bharath 2011-01-19 14:39:12

+0

我編輯了我原來的帖子。 – Odyss3us 2011-01-19 14:43:59

回答

7

如果沒有特定的錯誤信息產生很難肯定,但我懷疑這個問題很簡單,就是的定義被稱爲「LOGIN」,但你嘗試使用「USER」中的mysql_connect通話。

1

也許嘗試改變$連接=的mysql_connect(HOST,USER,PASS)由$連接=的mysql_connect(HOST,LOGIN,PASS)因爲你不定義用戶...;)

0
// Constants.php 
<?php 

final class Constants 
{ 
    const DEBUG      = true; 

    const DB_CHARSET    = "latin1"; 
    const DB_PORT     = 3306; 
    const DB_TIMEOUT    = 15; 
    const DB_HOST     = "localhost"; 
    const DB_DATABASE    = "foo_db"; 
    const DB_USER     = "foo_dbo"; 
    const DB_PASS     = "pass"; 

    private function __construct(){} 
} 
?> 

// usage: 

require_once "Constants.php"; 

if(Constants::DEBUG){..}