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.

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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能