gpt4 book ai didi

PHP 关闭 mysqli 与数据库的连接错误

转载 作者:行者123 更新时间:2023-11-29 20:27:12 25 4
gpt4 key购买 nike

在尝试解决我的问题时,我遇到此错误的情况与我在 SO 上看到的情况不同。我有一个类,Database,它只创建它自己的一个实例,试图将与 mysql 服务器的连接数量限制为 1。这是我的类(class)[代码 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);

执行此操作时,出现错误:

Warning: mysqli::query(): Couldn't fetch mysqli in /home/barbu/blog/admin/index.php on line 24

如果我不关闭第一个连接(假设我只让 ['unset($db)']),我的查询将在第一个数据库 ['siteinfo'] 上执行,我得到另一条错误消息,即告诉我“siteinfo”数据库中不存在“postInfo”表,这是真的。如果我让该连接持续存在,并声明数据库类的另一个实例 $db1 和另一个 mysqli 对象 $mysqli1(它保存我的连接),并通过它执行我的查询,我会得到与第二种情况相同的 mysqli 错误消息:“siteinfo.postInfo”不存在。你推荐我什么?我该如何解决这个问题?

最佳答案

首先,如果您希望每个 session 只有一个连接并且不允许创建第二个实例,您应该将 Database::__construct 定义为私有(private)。然后添加一个新方法Database::close。此方法的思想是关闭连接并将到类 Database 实例的链接设置为 null。代码如下所示:

public function close() 
{
if (self::$_instance) {
self::$_instance->getConnection()->close();
self::$_instance = null;
}
}

最后一点,您应该调用 $db->close(); 而不是 $mysqli->close();

关于PHP 关闭 mysqli 与数据库的连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39238162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com