我在PHP中创建了一个数据库类,我只想要OOP样式而不是程序样式。这是我试图关闭这个错误的代码。此功能导致问题。
class MySQLDatabase
{
private $connection;
public $message;
/**
* MySQLDatabase constructor.
*/
public function __construct()
{
$this->open_connection();
}
//Object oriented style
public function open_connection()
{
// the constant comes form config
$this->connection = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
/*
* This is the "official" OO way to do it,
*/
if ($this->connection->connect_error) {
die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
} else {
$this->message = "Success... " . $this->connection->host_info;
}
}
public function close_connection(){
if (isset($this->connection)) {
mysqli_close($this->connection);
unset($this->connection);
}
}
}
答案 0 :(得分:1)
public function close_connection()
{
if(isset($this->connection))
{
$this->connection->close();
unset($this->connection);
}
}
答案 1 :(得分:-1)
您正在以面向对象的方式创建新连接,因此您需要在连接对象上调用它的close方法。
所以你的代码看起来像这样,
public function close_connection()
{
if(isset($this->connection))
{
$this->connection->close();
unset($this->connection);
}
}