简体   繁体   English

如何在 OOPS 中的 php mysql 中断开或关闭与 DB 的数据库连接

[英]How to disconnect or close database connection from DB in php mysql in OOPS

Consider 3 classes DBCONNECT , book , new考虑 3 个类DBCONNECTbooknew

class dbconnect {
    protected $db_conn;
    public $db_user='xxxx';
    public $db_pass='xxxx';
    public $db_host='localhost';
    public $db_name='xxxx';

function connect() {
        try{
            $this->db_conn=new PDO("mysql:host=$this->db_host;dbname=$this->db_name",$this->db_user,$this->db_pass);
            return $this->db_conn;
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }
}


include_once ( 'class.dbconn.php' );

class Book{
    public $link;

    public function __construct(){
        $db_conn=new dbconnect();
        $this->link = $db_conn->connect();
        return $this->link;
    }
}

class new{
include_once 'classes/class.book.php';
$book = new Book();
}

everything works fine there were no errors in my code but I have a doubt of closing the connection to my database.一切正常,我的代码中没有错误,但我怀疑关闭与我的数据库的连接。 Is it mandatory to close a connection?关闭连接是强制性的吗? If it's mandatory, then how do I close the connection and in which class I need to write the code?如果它是强制性的,那么我如何关闭连接以及我需要在哪个类中编写代码?

It's not mandatory to close the connection manually.手动关闭连接不是强制性的。 PHP will handle that on it's own. PHP 会自行处理。 If you want to manually close it you can use $this->db_conn = null;如果你想手动关闭它,你可以使用$this->db_conn = null;

public function disconnect() {
    $this->db_conn = null
}

If you want to be a perfectionist, you could also do this, although not necessary如果你想成为一个完美主义者,你也可以这样做,虽然不是必须的

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

your new code你的新代码

<?php
class dbconnect {
    protected $db_conn;
    public $db_user='xxxx';
    public $db_pass='xxxx';
    public $db_host='localhost';
    public $db_name='xxxx';

function connect() {
        try{
            $this->db_conn=new PDO("mysql:host=$this->db_host;dbname=$this->db_name",$this->db_user,$this->db_pass);
            return $this->db_conn;
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }

function disconnect()
    {
            try
            {
                $this->db_conn=null;
                return $this->db_conn;
            }
            catch (Exception $e){
                return $e->getMessage();
            }
            }
    }
}


include_once ( 'class.dbconn.php' );

class Book{
    public $link;

    public function __construct(){
        $db_conn=new dbconnect();
        $this->link = $db_conn->connect();
        return $this->link;
    }
}

class new{
include_once 'classes/class.book.php';
$book = new Book();

$close = new dbconnect();
// this will close connection
$close->disconnect();


}?>

one more thing is you can create constructor and destructor.另一件事是您可以创建构造函数和析构函数。

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

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