drxm5014 2014-02-04 08:03
浏览 62
已采纳

如果未指定mysql链接,mysql函数是否会打开一个新的mysql连接?

Do functions like mysql_query, mysql_real_escape or mysql_error open a new mysql link if it's not specified ?

php.net says that

The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed

But what if the connection was made inside a class ? Does php know how to find that link ?

(I'm using the codeigniter framework if that's of any help)

  • 写回答

1条回答

  • dongqidi2799 2014-02-04 08:48
    关注

    What you are actually asking is if your database connection will be made in respect to the Singleton Pattern or not, and the answer is obviously "no". If you instantiate the class in multiple places, each instance will have its own resource. As a consequence, what this mean is that regardless of the instance from which you call mysql_query, it will use the last instance that will be detected, which is actually created during your last instantiation of the class that makes the connection.

    [Later edit]

    You will get into that situation if you have something similar with:

        //Database.class.php
    
        class Database {
            public function connect_db() {
                 //connection logic here
            }
    
            public static function query_db($query) {
                 $this->connect_db();
                 //query database logic here
            }
    
        }
    
    
        //index.php
        include "path/to/Database.class.php";
    
        $sql = "SELECT * FROM `whatever_i_want`";
        Database::query_db($sql);
    
        //repeat the last two lines multiple times
    

    Now, this is bad design. In this scenario you will instantiate a connection each time you query the database. To avoid this, the following small change should do the trick:

        //Database.class.php
        public function __construct() {
             $this->connect_db();
        }
    
        public function query_db($query) {
              //query database logic here
              //note the missing call to connect_db()
        }
        //the rest remains unchanged
    
        //index.php
        $db = new Database();
    
        $db->query_db($sql);
        //the rest remains unchanged
    

    Please bear in mind that this is not Singleton, the pattern I previously told you about, and it's not good design either. This is just for the purpose of showing how PHP decides which connection to use. The last example makes the database connection only once, and after that, no matter how many queries you run, it'll use the connection resource got from instantiating the class.

    Now, if we modify the last example and we add this to index.php:

        //index.php
        $db2 = new Database();
    
        $db2->query_db($sql);
        $db->query_db($sql);
    

    Both queries will use the resource got from creating the object $db2, since that will be the last one that PHP will detect.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?