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

如果没有指定mysql_query,mysql_real_escape或mysql_error这样的函数会打开一个新的mysql链接吗?</ p>

php.net说</ p>


MySQL连接。 如果未指定链接标识符,则假定mysql_connect()打开的最后一个链接</ p>
</ blockquote>

但如果连接是在类中进行的,该怎么办? php知道如何找到该链接吗?</ p>

(我正在使用codeigniter框架,如果有任何帮助的话)</ p>
</ div>

展开原文

原文

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)

dowm41315
dowm41315 完整引用(php.net/manual/en/function.mysql-query.php):如果未指定链接标识符,则假定mysql_connect()打开的最后一个链接。如果没有找到这样的链接,它将尝试创建一个,就好像没有参数调用mysql_connect()一样。如果未找到或建立连接,则会生成E_WARNING级别错误。
6 年多之前 回复
dqiatvbi61502
dqiatvbi61502 PHP知道如何找到链接,因为链接本身不在类中,这就是代码在创建链接时的位置
6 年多之前 回复

1个回答

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.

douti19680318
douti19680318 让我举个例子说明我的情况。 类db连接被实例化并且工作正常,我的问题是如果我使用没有链接的mysql_query,它是使用相同的资源还是打开新连接? 它工作,我已经测试,查询工作正常但我只是想避免这个dev.mysql.com/doc/refman/5.5/en/too-many-connections.html
6 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问