[英]PHP closing mysqli connection to database error
我遇到此錯誤的情況與我在嘗試解決我的問題時看到的情況有所不同。 我有一個數據庫類,它僅創建其自身的一個實例,試圖將與mysql服務器的連接數限制為一個。 這是我的課程[代碼1] :
class Database {
private $_connection;
// Store the single instance.
private static $_instance;
/**
* Get self instance of database to private static variable $_instance.
* @param string $host
* @param string $username
* @param string $password
* @param string $database
* @return Database
*/
public static function getInstance($host,$username,$password,$database) {
if (!self::$_instance) {
self::$_instance = new self($host,$username,$password,$database);
}
return self::$_instance;
}
/**
* Constructor.
* @param string $host
* @param string $username
* @param string $password
* @param string $database
*/
public function __construct($host,$username,$password,$database) {
$this->_connection = new mysqli($host, $username, $password, $database);
// 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;
}
}
上完本課之后,我創建了一個用於從表中獲取一些信息的連接。 代碼如下[代碼2] :
// Establish a connection with MySQL Server database.
$host = 'localhost';
$username = 'barbu';
$password = 'watcrn0y';
$database = 'siteinfo';
$db = Database::getInstance($host, $username, $password, $database);
$mysqli = $db->getConnection();
// Get the firstname of the author of the site from database.
$sql_query = 'SELECT author.firstname, author.lastname ';
$sql_query .= 'FROM author;';
$result = $mysqli->query($sql_query);
if($result && $row = $result->fetch_assoc()){
$author_firstname = $row['firstname'];
$author_lastname = $row['lastname'];
}
現在,在另一個文件中,我執行此操作[代碼3] :
require '../includes/siteinfo.php'; // this file contains the connection
// with first database ['siteinfo'].
//I include this file for accessing some other variables from it, which aren't
//in the code posted above.
// Establish the connection to server.
$host = 'localhost';
$username = 'barbu';
$password = 'watcrn0y';
$database = 'articles';
// Here I need to close my previous connection and begin another.
// It is important to remember that my class is designed for creating
// just one connection at a time.
// So if I want to change the connection to another database,
// I have to close the previous one and create the one which
// suits my needs.
$mysqli->close();
unset($db);
$db = Database::getInstance($host, $username, $password, $database);
$mysqli = $db->getConnection();
// Send the post info to the server.
$title = (isset($_POST['title'])) ? $_POST['title'] : '';
$text = (isset($_POST['text'])) ? $_POST['text'] : '';
$sql = "INSERT INTO postInfo ";
$sql .= "VALUES ('" . $author_firstname . " " . $author_lastname ."', '" .
date('d D M Y') . "', '" . $title . "', '" . $text . "');";
$result = $mysqli->query($sql);
在執行此操作時,出現錯誤:
警告:mysqli :: query():無法在第24行的/home/barbu/blog/admin/index.php中獲取mysqli
如果我不關閉第一個連接(並且假設我只允許['unset($ db)']),我的查詢將在第一個數據庫['siteinfo']上執行,並且我收到另一個錯誤消息,也就是說,這告訴我'siteinfo'數據庫中不存在'postInfo'表,這是事實。 如果我讓該連接保持不變,並聲明另一個數據庫類實例$ db1和另一個mysqli對象$ mysqli1,它們保存我的連接,並通過它執行查詢,則我將得到與第二種情況相同的mysqli錯誤消息:“ siteinfo.postInfo”不存在。 你對我有什么要求? 我怎么解決這個問題?
首先,如果您希望每個會話只有一個連接並且不允許創建第二個實例,則應將Database::__construct
定義為private。 然后添加一個新方法Database::close
。 此方法的想法是關閉連接,並將到類Database的實例的鏈接設置為null。 該代碼將如下所示:
public function close()
{
if (self::$_instance) {
self::$_instance->getConnection()->close();
self::$_instance = null;
}
}
最后一點,而不是$mysqli->close();
您應該調用$db->close();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.