我通过使用此单例类来获取此错误。请解释一下我在做错了什么?我试图通过构造函数获取sqli对象。
致命错误:从第20行的C:\ xampp \ htdocs \ docs \ users.php中的无效上下文调用私有DB :: __ construct()
<?php
class DB
{
#############################
##### Define Connection ####
#############################
const HOST = 'localhost';
const USER = 'root';
const PASSWORD = '';
const DATABASE = 'ali';
#############################
// Data Member For MySQLi Database Connection
private static $mysqli = NULL;
// Database Connection
private function __construct()
{
@self::$mysqli = new mysqli(self::HOST, self::USER, self::PASSWORD, self::DATABASE);
if (self::$mysqli->connect_error) {
throw new Exception("<b>Database Connect Error: </b>" . self::$mysqli->connect_error);
}
if (self::$mysqli->error) {
throw new Exception("<b>Database Select Error: </b>" . self::$mysqli->error);
}
echo "<span style='color: green; font-size: 18px; font-weight: bold'>Connected</span>
<br>"; // For testing //
return self::$mysqli;
}
// Class Level Static Function to Get Database Connection
public static function get_connection()
{
if ( ! isset(self::$mysqli) )
{
self::$mysqli = new self();
}
return self::$mysqli;
}
}
?>
我的第二个文件,我正在使用这个类
<?php
require_once 'DB.php';
class users extends DB
{
public function get_results()
{
$sqli = DB::get_connection();
$user_data = $sqli->query("SELECT * FROM `users`");
while($row = $user_data->fetch_object())
{
echo $row->name . "<br>";
}
}
}
$user = new users;
try {
$user->get_results();
} catch (Exception $e) {
echo $e->getMessage();
}
?>
答案 0 :(得分:2)
如果您不打算构建典型的数据库连接单例,那么下面的代码将实现您想要的目标
class DB
{
const HOST = 'localhost';
const USER = 'root';
const PASSWORD = '';
const DATABASE = 'yourdb';
private static $mysqli = null;
private function __construct() {
//an empty private constructor to prevent creating instances of this class
}
public static function get_connection() {
if(!self::$mysqli) {
//create mysqli object here and return instead of (typically returned) self instance
$conn = new mysqli(self::HOST, self::USER, self::PASSWORD, self::DATABASE);
if($conn->connect_error) {
throw new Exception("<b>Database Connect Error: </b>" . $conn->connect_error);
}
if($conn->error) {
throw new Exception("<b>Database Select Error: </b>" . $conn->error);
}
self::$mysqli = $conn;
}
return self::$mysqli;
}
}
除了删除extends DB
之外,用户类不需要进行任何更改,以消除错误。
更新
class DB
{
const HOST = 'localhost';
const USER = 'root';
const PASSWORD = '';
const DATABASE = 'yourdb';
private $mysqli = null;
private function __construct() {
$conn = new mysqli(self::HOST, self::USER, self::PASSWORD, self::DATABASE);
if($conn->connect_error) {
throw new Exception("<b>Database Connect Error: </b>" . $conn->connect_error);
}
if($conn->error) {
throw new Exception("<b>Database Select Error: </b>" . $conn->error);
}
$this->mysqli = $conn;
}
/**
* @return DB
*/
public static function get_connection() {
/**
* @static $DB DB a static variable that is not class member but will be _shared_ across all calls to DB::get_connection()
*
* WTF? http://stackoverflow.com/questions/6601027/php-and-static-variables-in-object-member-functions
*/
static $DB = null;
if($DB == null) {
$DB = new DB();
}
return $DB;
}
/**
* A wrapper for internal database object/connection/resource's query method
* @param $sql String the sql query
*
* @return bool|mysqli_result
*/
public function query($sql) {
return $this->mysqli->query($sql);
}
}