避免与db类的多个mysqli连接(avoid multiple mysqli connection with db class)

编程入门 行业动态 更新时间:2024-10-11 07:31:04
避免与db类的多个mysqli连接(avoid multiple mysqli connection with db class)

我刚刚开始oop,现在,甚至非常简单的事情让我感到困惑! 这是我的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); } }

让我们说我会像这样使用它:

$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);

我刚从DB类创建了2个对象。 这是否意味着每个对象都有一个新的mysql连接? 如果是的话,我该如何避免呢?

我知道我可以在__destruct()函数中使用mysqli_close(),但我读到某处(可能在这个网站:) :)创建和销毁几个mysql连接并不好。

我该怎么办? ps:旁边,就像我说我是oop的新手(以及mysqli)所以如果对我的课程有任何评论(例如我在正确的地方设置了charset?)我会很荣幸的。

i just started oop and now, even very simple things seem confusing to me! this is my class for mysqli connection:

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. does it mean there is a new mysql connection for each object? 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.

what should i do? p.s: 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.

最满意答案

在创建类的实例时,如果它正常工作,则无需重新创建它。

例1

在你的问题中你做到了这一点:

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

你可以这样做:

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

例2

有时你有一个实例,你想在任何地方使用它。 您可以在代码开头或第一次请求时创建它。 然后你可以随处使用它。

$DB = new DB(); function something(){ global $DB; $test = $DB->query($query); }

如果你的课程很简单,就像你在这里提到的那样,就像第一个例子一样使用它。 此方法适用于需要从任何位置访问的实例。 一个函数在其中更改某些内容,另一个函数使用该更改。

此方法也被认为是不好的做法,因为它使您的代码更难以阅读和调试。

一些有用的链接:

持久数据库连接 在PDO中使用持久连接有什么缺点 持久数据库连接 - 是或否? Global或Singleton用于数据库连接? 设置一个PHP对象全局? 如何将全局变量传递给PHP中的类? 如何创建一个全局可访问的对象 在PHP5中创建Singleton设计模式 PHP单例类的最佳实践 设计模式

When you are creating an instance of a class, you don't need to recreate it if it works properly.

Example 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

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:

Persistent Database Connections What are the disadvantages of using persistent connection in PDO Persistent DB Connections - Yea or Nay? Global or Singleton for database connection? Set a PHP object global? How to pass Global variables to classes in PHP? How to make a globally accessible object Creating the Singleton design pattern in PHP5 Best practice on PHP singleton classes Design Patterns

更多推荐

本文发布于:2023-07-17 14:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1145635.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   mysqli   db   avoid   class

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!