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 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝