简体   繁体   English

避免与db类建立多个mysqli连接

[英]avoid multiple mysqli connection with db class

i just started oop and now, even very simple things seem confusing to me! 我才刚刚开始,现在,即使是非常简单的事情也让我感到困惑! this is my class for mysqli connection: 这是我的mysqli连接类:

class DB{
    private $con;
    public function __construct(){
        $this->con=new mysqli('localhost','root','','dbName');
        if(!$this->con){
            echo '<b>There was a problem connecting to database! </b><br />errno: '.$con->connect_errno;
            exit;
        }
        $this->con->set_charset("utf8");
    }
    public function query($query){
        return $this->con->query($query);
    }
}

let's say i'm gonna use that like this: 假设我要这样使用:

$a=mysqli_real_escape_string($_POST['a']);
$b=mysqli_real_escape_string($_POST['b']);

$query='SELECT `name` FROM `someTable` WHERE `type`={'.$a.'}';
$query2='SELECT `name` FROM `someTable` WHERE `type`={'.$b.'}';

$DB = NEW DB;
$test=$DB->query($query);

$DB2 = NEW DB;
$test2=$DB2->query($query2);

i just created 2 objects from class DB. 我刚刚从类DB创建2个对象。 does it mean there is a new mysql connection for each object? 这是否意味着每个对象都有一个新的mysql连接? if yes, how can i avoid it? 如果是,我如何避免呢?

i know i can use mysqli_close() in __destruct() function but i read somewhere (probably in this very site :) ) that creating and destroying several mysql connections isn't good. 我知道我可以在__destruct()函数中使用mysqli_close(),但我读到某个地方(可能在此站点:))创建和销毁多个mysql连接不是很好。

what should i do? 我该怎么办? ps: beside, just as i said i'm new in oop (and mysqli as well) so if there is any comment about my class (for example did i set the charset at the right place?) i'll be honored. ps:此外,正如我所说的,我在oop(以及mysqli)中是新手,所以如果对我的课程有任何评论(例如,我是否将字符集设置在正确的位置?),我将很荣幸。

When you are creating an instance of a class, you don't need to recreate it if it works properly. 创建类的实例时,如果它可以正常工作,则无需重新创建它。

Example 1 例子1

in your question you did this: 在您的问题中,您这样做:

$DB = new DB();
$test=$DB->query($query);

$DB2 = new DB();
$test2=$DB2->query($query2);

You can do this instead: 您可以改为:

$DB = new DB();
$test = $DB->query($query);
$test2 = $DB->query($query2);

Example 2 例子2

Sometimes you have an instance and you want to use it everywhere. 有时您有一个实例,想在任何地方使用它。 you can create it at the start of your code or at the first request. 您可以在代码的开头或第一个请求处创建它。 and then you can use it everywhere. 然后您可以在任何地方使用它。

$DB = new DB();

function something(){

    global $DB;

    $test = $DB->query($query);
}

If your class is simple as you mentioned here, just use it like the first example. 如果您的班级很简单,如第一个示例所示,请使用它。 This method is for an instance that need to access from everywhere. 此方法适用于需要从任何地方访问的实例。 one function change something in it and another function using that change. 一个函数更改其中的某些内容,而另一个函数使用该更改。

Also this method consider as bad practice because it makes your code much harder to read and debug. 同样,此方法也被视为不好的做法,因为它会使您的代码更难阅读和调试。

Some useful links: 一些有用的链接:

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

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