gpt4 book ai didi

php mysql 包装器类 __destruct 方法无法关闭数据库

转载 作者:行者123 更新时间:2023-11-29 05:41:53 25 4
gpt4 key购买 nike

谁能解释为什么 mysql_close() 从类析构函数调用时失败? mysql_error() 报告“连接关闭失败。”

<?php
class Database
{
private $link_id = 0;
private $query_id = 0;

public $error = "";
public $errorNumber = 0;
public $affectedRows = 0;

public function __construct($server, $user, $pass, $database)
{
$this->link_id = @mysql_connect($server, $user, $pass, false);

if (!$this->link_id)
{
$this->DisplayError("Could not connect to server: <b>$this->server</b>.");
die(mysql_error());
}

if(!@mysql_select_db($database, $this->link_id))
{
$this->DisplayError("Could not open database: <b>$this->database</b>.");
die(mysql_error());
}
}

public function __destruct()
{
if ($this->link_id)
{
if(!@mysql_close($this->link_id))
{
$this->DisplayError("Connection close failed.");
die(mysql_error());
}
}
}

最佳答案

根据错误报告 #27903 ,由于脚本执行结束调用类析构函数时,似乎已经清理了一些资源。

无论如何,如 manual 中所示...

Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution.

关于php mysql 包装器类 __destruct 方法无法关闭数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6221779/

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