Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP PDO连接方法和MySQL中止客户端,中止连接值_Php_Mysql_Pdo - Fatal编程技术网

PHP PDO连接方法和MySQL中止客户端,中止连接值

PHP PDO连接方法和MySQL中止客户端,中止连接值,php,mysql,pdo,Php,Mysql,Pdo,在一个特定的PHP-MySQL应用程序中,我一直遵循在公共方法中从创建和销毁PDO连接的实践 例如: public function updateCustomerNumber($customer_id, $customer_number){ $conn = new database_class(); $sql = "UPDATE customers set number = :CUSTOMER_NUMBER whe

在一个特定的PHP-MySQL应用程序中,我一直遵循在公共方法中从创建和销毁PDO连接的实践

例如:

public function updateCustomerNumber($customer_id, $customer_number){
                $conn = new database_class();        
                $sql = "UPDATE customers set  number = :CUSTOMER_NUMBER where id = :CUSTOMER_ID";
                $query = $db->prepare($sql);
                $query->execute(array(':CUSTOMER_NUMBER' => $customer_number, ':CUSTOMER_ID'=>$customer_id));
                $conn->disconnect();
                if($query->rowCount()>0){
                    return true;
                } else {
                    return false;
                }
            }
因此,我在方法中连接和断开了PDO连接。数据库连接方法:

public function connect() {
        if (!$this->con) {
            try {
                $this->db = new PDO("mysql:host=$this->db_host;dbname=$this->db_name;charset=utf8", $this->db_user,$this->db_pass);
                $this->db->exec("SET CHARACTER SET utf8");
                $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $this->con = true;
                return $this->db;
            } catch (PDOException $e) {
                $error = $e->getMessage();
                echo $error;
            }
        } else {
            return $this->db;
        }
    }
public function disconnect() {
        if ($this->con) {
            unset($this->db);
            $this->con = NULL;
            return true;
        } else {
            return false;
        }
    }
数据库断开连接方法:

public function connect() {
        if (!$this->con) {
            try {
                $this->db = new PDO("mysql:host=$this->db_host;dbname=$this->db_name;charset=utf8", $this->db_user,$this->db_pass);
                $this->db->exec("SET CHARACTER SET utf8");
                $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $this->con = true;
                return $this->db;
            } catch (PDOException $e) {
                $error = $e->getMessage();
                echo $error;
            }
        } else {
            return $this->db;
        }
    }
public function disconnect() {
        if ($this->con) {
            unset($this->db);
            $this->con = NULL;
            return true;
        } else {
            return false;
        }
    }
到目前为止,我还没有遇到任何问题,除了在一些方法中,我忘记在脚本末尾断开PDO连接。然而,我不确定采用这种方法是否有任何缺点

我开始质疑这个方法的原因是当我检查MySQL全局状态值时

中止的_客户端:32560 中止连接:128080

根据定义,即使我们没有显式关闭连接,PHP也会在脚本结束时自动关闭连接


这些数字背后的原因可能是什么?这种打开和关闭PHP PDO连接的方法正确吗

在断开连接之前,您是否
closeCursor
?我建议您使用外部库,如@frz3993 I disconnect,然后再从方法返回任何内容。因此,在关闭连接之前,查询数据的所有其他操作都会完成。在断开连接之前,您是否
closeCursor
?我建议您使用外部库,如?@frz3993 I disconnect,然后再从方法返回任何内容。因此,查询数据的所有其他操作都是在关闭连接之前完成的。