PHP database connection The difference and usage of MySQL and mysqli

Source: Internet
Author: User
Tags pconnect php and mysql php database

The MYSQLI connection is a permanent connection, and MySQL is a non-permanent connection.

MySQL connection: Whenever a second use occurs, a new process is reopened.

MYSQLI Connection: Always use only the same process.

Benefits: This can reduce server-side stress to a great extent.

Of course, if MySQL also needs a permanent connection, you can use the Mysql_pconnect () function

Process-oriented use of mysqli:

$conn = Mysqli_connect (' localhost ', ' root ', ' 123′, ' db_test ') or (' Error ');

$sql = "SELECT * from db_table";

$query = Mysqli_query ($conn, $sql);

while ($row = Mysqli_fetch_array ($query)) {

echo $row [' title '];

}

Object-oriented use of mysqli:

$conn = mysqli (' localhost ', ' root ', ' 123′, ' db_test ');

$sql = "SELECT * from db_table";

$query = $conn->query ($sql);

while ($row = $query->fetch_array ()) {

echo $row [' title '];

}

    • Mysql_connect and Mysql_pconnect and Mysqli_connect:

Mysql_pconnect The open connection is not closed (even if the call to Mysql_close is not closed because it is not valid),

Similar to the connection buffer pool, if the next time there is the same user name from the same machine

For a connection to the same database, PHP automatically uses the last connection that was established, without having to re-establish one.

Benefit: eliminates the overhead of establishing a connection to the database each time,

Cons: It is necessary to waste some memory, occupy some connections,

So if the user access to a large number of errors will occur, to the MySQL max_connections parameter to be larger, or use mysql_connect () to solve the problem.

In short, mysql_pconnect is used to build a continuous connection between PHP and MySQL,

The general PHP execution pattern is that all resources are initialized when the script starts executing, and all resources are freed after the script is run.

And Mysql_pconnect Way is not so, mysql_connect each time is re-through TCP and so on

A relationship with a SQL Server, each connection is to consume a lot of server resources.

When using Pconnect, when there is a request to connect to MySQL, PHP will check whether a previous connection (with the same user name password connected to the same MySQL server) has been established,

If there is a direct use of this connection, it is worth noting that the concept of the same connection is for the process,

Multiple connections are established when different processes connect mysql_pconnect.

Connect and Pconnect do not bring functional differences, only performance differences.

PHP generally has two modes of operation, one is to run as a CGI, and the other is to run as an Apache module.

As CGI, connect is no different from pconnect, because every time the CGI runs, it is destroyed to clean up resources.

When PHP runs as an Apache module, you can use persistent connections to the database, but there may be potential problems.

Look at the PHP manual if you are the CGI installation method. Pconnection will never take effect.

The biggest disadvantage of long connections is that in case a user locks up, the current process is permanently locked.

If your setup in Apache is that the process will never be destroyed ..... .......

Said, and recorded so much,

    1. In other words, use mysql_connect as much as possible, because the end of the run will be interrupted automatically, in line with the programming style bar.
    2. It can also be used with mysql_connect and mysql_pconnect, just like a linked buffer pool,
    3. That is to say, create a class with Mysql_connect and Mysql_pconnect.
    4. Of course, if backward compatibility is not considered, it is best to use mysqli_connect, because mysqli itself is a permanent connection.

As for the advantages of mysqli, we do not introduce more.

Transfer from http://blog.csdn.net/enough_br/article/details/8773773

PHP database connection The difference and usage of MySQL and mysqli

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.