简体   繁体   English

关闭我的MySQL连接

[英]Closing my mySQL connection

I'm a .Net developer that have taken over a PHP project. 我是一位接管PHP项目的.Net开发人员。 This project has a database layer that looks like this: 该项目具有一个数据库层,如下所示:

<?php

class DatabaseManager {

    private static $connection;

    const host = "projectname.mysql.hostname.se";
    const database = "databaseName";
    const username = "userName";
    const password = "password";

    public function __construct()
    {

    }

    public function instance($_host = null, $_username = null, $_password = null)
    {
        if(!self::$connection)
        {
            if(!$_host || !$_username || !$_password)
            {
                $host = self::host;
                $username = self::username;
                $password = self::password;         
            }else{
                $host = $_host;
                $username = $_username;
                $password = $_password;
            }
            self::$connection = mysql_connect($host, $username, $password);
            $this->setDatabase();
        }
        return self::$connection;
    }

    public function setDatabase($_database = null)
    {
        if(!$_database)
        {
            $database = self::database;
        }else{
            $database = $_database;
        }
        $connection = $this->instance();
        mysql_select_db($database, $connection) or die(mysql_error());  
    }
} ?>

I have written a php file that uses this layer but after a while i got these mysql errors implying i didn't close my connections which i hadn't. 我已经写了一个使用此层的php文件,但过了一会儿,我得到了这些mysql错误,表明我没有关闭我没有的连接。 I try to close them but know i get other weird errors like system error: 111 . 我尝试关闭它们,但知道我遇到其他奇怪的错误,例如系统错误:111 Very simplyfied my php file looks like this: 非常简单,我的php文件如下所示:

<?php
$return = new stdClass();

$uid = '9999999999999';
$return->{"myUid"} = $uid;

$dm = new DatabaseManager();
$dmInstance = $dm->instance();

/* MY CLICKS */
$sql = sprintf("SELECT count(*) as myClicks FROM clicks2011, users2011 WHERE clicks2011.uid = users2011.uid AND users2011.uid = %s AND DATEDIFF(DATE(at), '%s') = 0 AND exclude = 0", mysql_real_escape_string($uid), mysql_real_escape_string($selectedDay));
$result = mysql_query($sql, $dmInstance) or die (mysql_error());
$dbResult = mysql_fetch_row($result);
$return->{"myClicks"} = $dbResult[0];

mysql_close($dmInstance);

echo json_encode($return); ?>

Okay, I'm going to post this as an answer because I think one (possibly both) of these things will help you. 好的,我将其发布为答案,因为我认为这两种方法中的一种(可能同时适用)对您有帮助。

First: You don't need to manually close your MySQL connections . 首先: 您不需要手动关闭MySQL连接 Unless you have set them up so that they persist, they will close automatically. 除非您对其进行设置以使其持久存在,否则它们将自动关闭。 I would avoid doing that unless you determine that every other problem is NOT the solution. 除非您确定其他所有问题都不是解决方案,否则我将避免这样做。

In addition, I would switch to using prepared statements . 另外,我将切换为使用预处理语句 It's more secure, and pretty future-proof. 它更加安全,并且可以面向未来。 I prefer PHP's PDO over mysqli, but that's up to you. 与mysqli相比,我更喜欢PHP的PDO,但这取决于您。

If you'd like to look over an example of a simple PDO object to take the many lines out of creating prepared statements and connections and getting results, you can look at my homebrew solution . 如果您想查看一个简单的PDO对象的示例,以从创建准备好的语句和连接以及获得结果中去除很多内容 ,那么可以看看我的自制解决方案

Second: "System Error 111" is a MySQL error. 第二:“系统错误111”是MySQL错误。 From what I've read, it appears that this error typically occurs when you are using PHP and MySQL on the same server, but telling PHP to connect to MySQL via an IP address. 从我阅读的内容来看,当您在同一台服务器上使用PHP和MySQL时,通常会发生此错误,但告诉PHP通过IP地址连接到MySQL。 Switch your $host variable to 'localhost' . $host变量切换为'localhost' It is likely that this will solve that error. 这很可能会解决该错误。

The problem here is you're calling mysql_close and not specifying a valid mysql connection resource object. 这里的问题是您正在调用mysql_close而不指定有效的mysql连接资源对象。 You're, instead, trying to close an instance of the DatabaseManager object. 相反,您正在尝试关闭DatabaseManager对象的实例。

You'll probably want to run mysql_close(DatabaseManager::connection); 您可能要运行mysql_close(DatabaseManager :: connection); which is where the DatabaseManager is storing the resource object. 这是DatabaseManager存储资源对象的位置。

Additionally, I'd personally recommend you learn PDO or use the mysqli drivers. 另外,我个人建议您学习PDO或使用mysqli驱动程序。 In future releases of PHP the built in mysql functions will be moved into E_DEPRECATED 在将来的PHP版本中,内置的mysql函数将移至E_DEPRECATED

Try implement __destrcut 尝试实施__destrcut

public function __destruct()
{
    mysql_close(self::$connection)
}

Then simply use unset($dm); 然后只需使用unset($dm);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM