我正在创建一个新脚本,目前正在构建数据库类。我创建了一个检查用户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在数据库中,以防有人想知道。
答案 0 :(得分:1)
我会避免使用单例并在需要时创建一个Database
对象。而不是Database->checkIp('1')
你应该使用Database
的实例,而不是像这样调用函数更静态。此外,您的checkIp
功能应该只通过getConnection
访问$this
功能,因为您已经在课堂内并且不需要创建新实例。
您可能希望确保启用错误报告,以便更轻松地识别问题:
error_reporting(E_ALL);
ini_set('display_errors', 1);
<?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;
/**
* 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);
}
}
/**
* Get the mysqli connection.
*/
public function getConnection() {
return $this->_connection;
}
/**
* DATABASE IP CHECK FUNCTION
*/
public function checkIp($user_ip) {
$mysqli = $this->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!');
}
}
}
?>
用法:
<?php
require_once('class.Database.inc.php');
$database = new Database();
$database->checkIp('1');
?>