What is the difference between mysql and mysqli?

Source: Internet
Author: User
Tags pconnect

What is the difference between mysql and mysqli?
Mysqli connection is a permanent connection, while MySQL is a non-permanent connection.

Mysql connection: a new process is opened every second time.

Mysqli connection: only the same process is used all the time.

Benefit: this can greatly reduce the pressure on the server.

 

Of course, if mysql needs to be Connected permanently, you can use the mysql_pconnect () function.

 

Mysqli process-oriented usage:

$ Conn = mysqli_connect ('localhost', 'root', '000000', '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 usage of mysqli:

$ Conn = mysqli ('localhost', 'root', '000000', '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:

 

The connection opened by mysql_pconnect will not be closed (even if mysql_close is called, it will not be closed because it is invalid ),

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

For connections to the same database, PHP will automatically use the connection established last time, without the need to re-establish a new one.

Advantage: it saves the overhead of establishing a connection to the database every time,

Disadvantage: some memory needs to be wasted and some connections are occupied,

Therefore, if an error occurs when the user's access volume is large, you need to increase the max_connections parameter of mysql or use mysql_connect () to solve the problem.

 

In short, MySQL_pconnect is used to establish a persistent connection between php and MySQL,

Generally, the php Execution Mode initializes all resources when the script starts execution, and releases all resources after the script finishes running.

This is not the case with MySQL_pconnect. MySQL_connect reuses tcp and so on each time.

Establishing a relationship with the SQL server consumes a lot of server resources each connection.

 

When pconnect is used and a request is sent to connect to MySQL, php checks whether there is an identical connection (connected to the same MySQL server with the same user name and password,

If yes, use this connection directly. It is worth noting that the same connection concept is for the process,

Multiple connections are established when different processes connect to MySQL_pconnect.

Connect and pconnect do not bring about functional differences, but only performance differences.

Generally, php runs in two modes: cgi and apache.

As a cgi, connect is no different from pconnect, because every time cgi is run, resources are destroyed and cleared.

 

Php can be used for continuous database connection when running as an apache module, but there may be potential problems,

 

Check the php manual. If you are installing cgi. Pconnection will never take effect.

The biggest disadvantage of persistent connection is that the current process will be permanently locked if a user is locked.

If you set the process in apache to never be destroyed, then ..................

 

Said, and recorded so much,

As for the advantages of mysqli, I will not introduce it more.

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.