gpt4 book ai didi

PHP,用于处理 MySql 和 mysql_close 问题的简单类

转载 作者:行者123 更新时间:2023-11-29 03:46:41 24 4
gpt4 key购买 nike

这是一个用于处理数据库的简单 php 类。此类的问题在于 __destruct 方法,该方法启动 mysql_close 函数。如果我创建两个位于代码中非常接近的数据库对象,则会导致错误。看起来 mysql_close 关闭了所有数据库连接,而不仅仅是他自己的($this->dbLink)。为什么会这样?

    require_once("modules/required/OptionsReader/m_OptionsReader.php");

class DB{
private $dbLink;

public function __construct() {
$options = new OptionsReader();
$DBSettings = $options->getDBSettings();
$this->dbLink = mysql_connect($DBSettings->getAddress(),$DBSettings->getUserName(),$DBSettings->getPswd());
@mysql_select_db($DBSettings->getDBName()) or die( "Unable to select database");
mysql_query( "set names 'utf8'" );
}

public function __destruct() {
mysql_close($this->dbLink);
}


public function launchQuery($query){
return mysql_query($query);
}
}
?>

附言我知道这个问题的简单解决方案就是从 __desctuctor close() 函数创建并手动启动它,但了解发生了什么会很棒。谢谢

最佳答案

@zneak 是正确的。 mysql_connect 执行池连接。问题是,为什么要关闭连接呢?在我多年的 PHP 开发中,我用过 mysql_connect 的次数一只手都能数过来,我几乎只用过 mysql_pconnect

来自 http://php.net/mysql_pconnect

Establishes a persistent connection to a MySQL server.

mysql_pconnect() acts very much like mysql_connect() with two major differences.

First, when connecting, the function would first try to find a (persistent) link that's already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.

Second, the connection to the SQL server will not be closed when the execution of the script ends. Instead, the link will remain open for future use (mysql_close() will not close links established by mysql_pconnect()).

关于PHP,用于处理 MySql 和 mysql_close 问题的简单类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3472972/

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