Including PHP constants
I am attempting to create a database config file that contains DB details, and then include the file in the files that I will be doing queries on, but I keep getting errors, it seems to be taking the constant as a string instead of it's value.
Here is the config.php code:
define("DB", "db", true);
define("LOGIN", "login", true);
define("PASS", "pass", true);
define("HOST", "server.com", true);
and here is where I include and use the constants:
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());开发者_Python百科
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());
edit* Here is the error I am getting Access denied for user 'USER'@'100-200-0-200.dynamic.adsl.com' (using password: YES)
Any idea where I am going wrong? It's probably staring me in the face, but I just can't see it.
Thanx in advance!
Without the specific error message generated it's hard to be certain, but I suspect the issue is simply that the define is called "LOGIN", but you're attempting to use "USER" in the mysql_connect call.
Maybe try changing $connect = mysql_connect(HOST,USER,PASS) by $connect = mysql_connect(HOST,LOGIN,PASS) as you don't define USER... ;)
// 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){..}
精彩评论