我正在尝试构建一个连接到单个数据库的简单脚本。我想将类文件放到我的许多网站上,并希望能够在我的数据库中“注册”该网站。
为了避免必须为类的每个实例运行 SQL 查询,我想使用 PHP 常量“注册”域。
在我的方法中,我定义了常量并检查了它 - 它可以工作,但是当我在构造函数中检查常量时,不再定义常量。
这是我的类文件,我确定这只是我对常量不了解的东西。
<?php
/**
* @author bailz777
*
*
*/
class db_interface {
public $ServerName = 'localhost:3306'; //hostname:port
public $UserName = '******'; //mysql user
public $Password = '******'; //mysql password
public $DataBase = '******'; //database name
public $Domain = 'test.com'; //Full domain name (no host)
public $con = '';
function __construct() {
//on construction, we must ensure that the domain is registered in our system
//first check if it was defined locally to avoid extra DataBase Work
var_dump(defined('DOMAIN_REGISTERED'));
if(!defined('DOMAIN_REGISTERED')) {
$this->db_connect();
$result = $this->validate_domain();
if($result) {
echo "<p>Domain Validated!!</p>";
}
$this->db_disconnect();
}
else {
echo "<p>Domain Validated!!</p>";
}
}
/**
*
*/
function __destruct() {
}
/**
*
* @param unknown_type $domain
* @return boolean
*/
private function validate_domain() {
$constants = get_defined_constants();
// return $this->con;
// print_r($constants);
var_dump(defined('DOMAIN_REGISTERED'));
if(defined('DOMAIN_REGISTERED')) {//Check DOMAIN_REGISTERED to avoid unnecessary db work
return TRUE;
}
elseif (!defined('DOMAIN_REGISTERED')) {//Check the domain is in the db
echo '<p>Domain was not locally registered, checking DataBase</p>';
$query = "SELECT `name` FROM `$this->DataBase`.`registered_domains` WHERE `name` = '$this->Domain'";
$result = mysql_query($query,$this->con);
//var_dump($result);
if(!$result) {
die('No result found : ' . mysql_error());
}
elseif (mysql_num_rows($result)==0) { //if no rows returned, then domain is not in DataBase
$domain_exists = FALSE;
}
elseif (mysql_num_rows($result)>0) { //if rows returned, then domain is in DataBase
$domain_exists = TRUE;
//If a domain does not exist, a mysql will be passed, use @ to suppress the error
//The domain will be written to the db and on the next run of this function, the
//constant will be defined
}
if($domain_exists) {//If it exists Then assign CONSTANT DOMAIN_REGISTERED to TRUE
echo '<p>Domain Found in DataBase</p>';
echo '<p>Registering domain locally</p>';
define("DOMAIN_REGISTERED", TRUE);
if(DOMAIN_REGISTERED) {
echo '<p>Successfully registered domain locally</p>';
}
//var_dump(defined('DOMAIN_REGISTERED'));
//echo DOMAIN_REGISTERED;
return TRUE;
}
elseif(!$domain_exists) {//If it does not exist then add it to the registered_domains table, and assign CONSTANT __DOMAIN_TRUE__ to TRUE
echo '<p>Domain not found in DataBase</p>';
echo '<p>Now Registering Domain</p>';
$query = "INSERT INTO `$this->DataBase`.`registered_domains` (`name`) VALUES ('$this->Domain')";
$result = mysql_query($query);
if(!$result) {
die('Domain not added : ' . mysql_error());
}
else{
define("DOMAIN_REGISTERED", TRUE);
return TRUE;
}
}
}
}
//Connect to mysql and define the active database
private function db_connect() {
$this->con = $con = mysql_connect($this->ServerName,$this->UserName,$this->Password);
if (!$con) {
die('Could not connect: ' . mysql_error());
}
else {
echo 'Successfully connected to MySQL<br />';
//define active database
$this->db = mysql_select_db($this->DataBase);
if(!$this->db) {
die('Could not connect to Database : ' . mysql_error());
}
else {
echo 'Successfully connected to DataBase<br />';
}
}
}
//disconnect from mysql
private function db_disconnect() {
$close = mysql_close($this->con);
if($close) {
echo 'Successfully disconnected from MySQL<br />';
}
}
public function add_record($fname,$lname,$email) {
$ip = $_SERVER['REMOTE_ADDR'];
$authorized_date = time();
}
}
?>