Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between mysqli and mysql

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "what is the difference between mysqli and mysql". In daily operation, I believe that many people have doubts about the difference between mysqli and mysql. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what is the difference between mysqli and mysql?" Next, please follow the editor to study!

Differences: 1, the mysqli connection is a permanent connection, while the mysql is not a permanent connection; 2, every time the mysql connection is used for the second time, it will reopen a new process, while the mysqli connection always uses the same process.

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

1. The difference between mysql and mysqli:

Mysqli connections are permanent connections, while mysql connections are non-permanent connections.

Mysql connection: every time it is used for the second time, a new process will be reopened.

Mysqli connections: use only the same process all the time.

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

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

2. Use of mysqli:

1. 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'];}

2. 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'];}

3. Mysql_pconnect and mysqli_connect:

1. The connection opened by mysql_pconnect will not be closed (even if mysql_close is called, because it will not be closed because it is invalid), similar to the connection buffer pool. The next time there is a connection from the same user name from the same machine to the same database, php will automatically use the connection that was established last time, and there is no need to establish a new one.

Benefit: it saves the overhead of establishing a connection with the database each time.

The downside: it needs to waste some memory and take up some connections.

So if an error occurs when the user visits a lot, change the max_connections parameter of mysql a little larger, or use mysql_connect () to solve the problem.

2. To put it simply, MySQL_pconnect is used to establish a continuous connection between php and MySQL. The general execution mode of php is to initialize all resources when the script starts to execute, and to release all resources after the script ends.

This is not the case with MySQL_pconnect. Every time MySQL_connect re-establishes a relationship with the sql server through tcp, etc., and each connection consumes a lot of server resources.

3. When using pconnect, when there is a request to connect to MySQL, php will check whether the same connection (connecting to the same MySQL server with the same user name and password) has been established, and if so, use this connection directly. It is worth noting that the concept of the same connection is for the process, different process connection MySQL_pconnect establishment will establish multiple connections.

4. There is no functional difference between connect and pconnect, only the difference in performance.

5. Generally, there are two operation modes of php, one is to run as cgi, the other is to run as a module of apache.

6. As a cgi, connect is no different from pconnect, because every time cgi is run, it will be destroyed and clean up the resources.

At this point, the study of "what is the difference between mysqli and mysql" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report