我正在製作一個新腳本,目前正在構建數據庫類。我創建了一個檢查用戶IP地址的函數。當我去測試它時,我留下了一個空白頁面,而不是die('It Worked');
。我的功能有什麼問題?
我不喜歡發佈代碼的負載,但如果我發佈我的類到目前爲止更有意義。
class.Database.inc
<?php
require_once('config.php'); // Configuration file
/**
* MySQLi database; only one connection is allowed.
*/
class Database {
// Database credentials from config file
private $_DATABASE_SERVER = DB_SERVER;
private $_DATABASE_USER = DB_USER;
private $_DATABASE_PASS = DB_PASS;
private $_DATABASE_NAME = DB_NAME;
private $_connection;
// Store the single instance.
private static $_instance;
/**
* Get an instance of the Database.
* @return Database
*/
public static function getInstance() {
if (!self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Constructor.
* Database connection (server, user, password, name)
*/
public function __construct() {
$this->_connection = new mysqli($this->_DATABASE_SERVER, $this->_DATABASE_USER, $this->_DATABASE_PASS, $this->_DATABASE_NAME);
// Error handling.
if (mysqli_connect_error()) {
trigger_error('Failed to connect to MySQL: ' . mysqli_connect_error(), E_USER_ERROR);
}
}
/**
* Empty clone magic method to prevent duplication.
*/
private function __clone() {}
/**
* Get the mysqli connection.
*/
public function getConnection() {
return $this->_connection;
}
/**
* DATABASE IP CHECK FUNCTION
*/
public function checkIp($user_ip) {
$db = self::getInstance();
$mysqli = $db->getConnection();
$sql_query = "SELECT ip FROM ";
$sql_query .= "ip_address WHERE ";
$sql_query .= "ip = '$user_ip'";
$result = $mysqli->query($sql_query) or die(mysqli_error($mysqli));
if ($row = $result->fetch_assoc()) {
die('It Worked!');
}
}
}
?>
問題是與位於朝向該文件的底部,checkIp();
函數的函數。
我已經創建了包含這些內容的test.php
文件。
<?php
require_once('class.Database.inc.php');
Database->checkIp('1');
?>
我不知道我是否試圖錯誤地訪問該函數或者它是否是別的東西。值1在數據庫中,以防有人想知道。
閱讀錯誤日誌。 –
單身。不要那樣做,只是...不要。 PS:類不會'死',很少'trigger_error',它們會拋出異常# –
@MartinBean在error_log中沒有任何內容 –