在我试图解决我的问题时,我收到此错误的情况与我在SO上看到的情况不同。我有一个类,Database,它只创建一个自己的实例,试图将与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;
}
}
在这堂课之后,我创建了一个连接,用于从表中获取一些信息。代码如下 [code 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'];
}
现在,在另一个文件中,我这样做 [code 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
如果我没有关闭第一个连接(假设我只是['取消设置($ db)']),我的查询将在第一个数据库上执行[&# 39; siteinfo'],我收到了另一条错误消息,即一个告诉我'postInfo'表格不存在于&site;      数据库,这是真的。如果我让该连接持久化,并声明另一个Database类的实例,$ db1和另一个mysqli对象,$ mysqli1,它保存我的连接,并通过它执行我的查询,我得到与第二种情况相同的mysqli错误消息:' siteinfo.postInfo'不存在。你有什么推荐给我的?我该如何解决这个问题?
答案 0 :(得分:0)
首先,如果您希望每个会话只有一个连接而不允许创建第二个实例,则应将Database::__construct
定义为私有。
然后添加一个新方法Database::close
。此方法的想法是关闭连接并将类Database的实例的链接设置为null。代码如下所示:
public function close()
{
if (self::$_instance) {
self::$_instance->getConnection()->close();
self::$_instance = null;
}
}
最后一点,您应该拨打$mysqli->close();
$db->close();