Differences between mysql and mysqli _ MySQL

Source: Internet
Author: User
Tags pconnect
Differences between mysql and mysqli bitsCN.com
Difference between mysql and mysqli: mysqli connections are permanent connections, while mysql does not. 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 also needs to be connected permanently, you can use the mysql_pconnect () function of mysqli for process-oriented usage: 01 $ conn = mysqli_connect ('localhost', 'root ', '123', 'DB _ test') or ('error'); 02 03 $ SQL = "select * from db_table"; 04 05 $ query = mysqli_query ($ conn, $ SQL); 06 07 while ($ row = mysqli_fetch_array ($ query) {08 09 echo $ row ['title']; 10 11} object-oriented usage of mysqli: 01 $ conn = mysqli ('localhost', 'root', '000000', 'DB _ test'); 02 03 $ SQL = "select * from db_table"; 04 05 $ query = $ conn-> query ($ SQL); 06 07 while ($ row = $ query-> fetch_array ()) {08 09 echo $ row ['title']; 10 11} connections opened between mysql_connect and mysql_pconnect and mysqli_connect: mysql_pconnect will not be closed (even if mysql_close is called, it is similar to the connection buffer pool. if there is a connection from the same user name on the same machine to the same database next time, php will automatically use the connection that has been established last time, you do not need to create another one. Advantage: it saves the overhead of establishing a connection to the database. The disadvantage is that some memory needs to be wasted and some connections are occupied. Therefore, if a user has a large access volume, an error may occur, change the max_connections parameter of mysql to a larger value, 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 is to initialize all resources when the script starts to be executed, and release all resources after the script ends. this is not the case with MySQL_pconnect. MySQL_connect re-establishes a relationship with the SQL Server through tcp and other methods. each connection consumes a lot of server resources. 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 processes. multiple connections will be 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 cg I connect is no different from pconnect, because every time the cgi is run, the resources will be destroyed and cleared. php can be used for continuous database connection when running as an apache module, but there may be potential problems. refer to the php manual if you are using cgi for installation. 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 .................. That is to say, use mysql_connect whenever possible, because the operation will be automatically interrupted after the end of the operation, in line with the programming style. You can also use mysql_connect and mysql_pconnect together, just like a connection buffer pool, that is, to create a class with mysql_connect and mysql_pconnect. Of course, if downward compatibility is not considered, it is best to use mysqli_connect, because mysqli itself is a permanent connection. As for the advantages of mysqli, I will not introduce it more. Author: Jiang Bian Wanghai bitsCN.com

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.