mysql - What is the best way to connect and release the database in PHP?

WBOY
Release: 2016-08-04 09:20:14
Original
1470 people have browsed it

In the manual, the connection is closed after each query is completed. So when there are multiple database operations, this will lead to constant generation and release of connections. Isn’t it unreasonable?

For example, there is a.php, which contains 20 functions, each of which needs to instantiate 20 objects, and each object has database operations.

So, there are three methods:
1. Obtain a database connection in each class of the 20 objects, the operation is completed and closed.
2. Obtain the database connection in a.php, pass the connection parameter in each function, and finally release it in a.php.
3. Generate a static database connection object and call ConnectionClass::db in each class

What is better, please discuss it.

.

Reply content:

In the manual, after each query is completed, the connection is closed. So when there are multiple database operations, this will lead to constant generation and release of connections. Isn’t it unreasonable?

For example, there is a.php, which contains 20 functions, each of which needs to instantiate 20 objects, and each object has database operations.

So, there are three methods:
1. Obtain a database connection in each class of the 20 objects, the operation is completed and closed.
2. Obtain the database connection in a.php, pass the connection parameter in each function, and finally release it in a.php.
3. Generate a static database connection object and call ConnectionClass::db in each class

What is better, please discuss it.

.

Using a static variable to save the database connection allows multiple functions to connect to the database only once in one request.
If the persistent connection is enabled, a PHP-FPM process can share a database connection in multiple requests.
mysql - What is the best way to connect and release the database in PHP?

<code><?php
function db() {
    static $db; //静态变量
    if ($db) {
        return $db;
    } else {
        $db = new mysqli('p:127.0.0.1','user','pass','dbname',3306);
        return $db;
    }
}
function foo1() {
    return db()->query('SELECT * FROM table1')->fetch_all();
}
function foo2() {
    return db()->query('SELECT * FROM table2')->fetch_all();
}
var_export( foo1() );
var_export( foo2() );</code>
Copy after login

@impig33 The following two function calls return the same result. It can be seen that after $db is assigned a value for the first time, the branch in else will not be executed the second time, and a uniqid number will not be generated.

<code><?php
header('Content-Type: text/plain; charset=utf-8');
function db() {
    static $db; //静态变量
    if ($db) { return $db; } 
    else { $db = uniqid(mt_rand(), true); return $db; }
}
function foo1() { return db(); }
function foo2() { return db(); }
echo foo1()."\n";
echo foo2()."\n";</code>
Copy after login

I don’t know about other databases, but MySQL supports persistent connections.
Continuous connection will not close the previous database connection, and the previous connection will be used directly the next time you connect.

MySQLi: There is no pconnect() function similar to MySQL, but you can add p: in front of the host name when connecting. Please refer to PHP official website: mysqli extension and persistent connection. Thanks to @eechen for the correction.

Unlike the mysql extension, mysqli does not provide a special method for opening a persistent connection. When you need to open a persistent connection, you must add p: before the host name when connecting.

PDO also supports long connections: use array(PDO::ATTR_PERSISTENT) in the fourth parameter of new PDO, refer to the PHP official manual - PDO connection and connection management Example4.

3. No need to discuss this. Usually Db objects adopt singleton mode, which is already a consensus.

Personally, I think 2 is the best.

You can also consider using a connection pool
https://www.baidu.com/s?wd=ph...

I also think 3 supports singleton mode

Don’t know how to write this answer.
A bunch of things: objects, destructors, singletons, dependency injection, persistent connections
Finally, I suddenly realized that this is not difficult, so I found a framework and analyzed it, such as phalcon.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!