MySQL is a non-holding relay function and MYSQLI is a permanent connection function. Other words
MySQL each link will open a connected process and mysqli run mysqli multiple times will use the same connection process, thereby reducing the cost of the server
Some friends use the new mysqli (' localhost ', usenamer ', ' Password ', ' DatabaseName ') when programming, and always error, Fatal error:class ' mysqli ' not found In D: ...
is the Mysqli class not PHP-led?
Not the default to open, win under to change php.ini, remove Php_mysqli.dll before;, Linux to the mysqli compiled into
Instance
Mysql.dll (can be understood as a functional approach):
| The code is as follows |
Copy Code |
$conn = mysql_connect (' localhost ', ' user ', ' password '); Connecting to the MySQL database mysql_select_db (' data_base '); Select Database $result = mysql_query (' select * from Data_base ');//Here is a second optional parameter that specifies an open connection $row = Mysql_fetch_row ($result))//For simplicity, only one row of data is taken here echo $row [0]; Output the value of the first field |
The Mysql_pconnect open connection does not close (even if the call Mysql_close does not close because it is not valid),
Similar to the connection buffer pool, if the next time there is a same user name from the same machine
For a connection to the same database, PHP automatically uses the previously established connection without having to re-establish one.
Benefits: Eliminates the cost of establishing a connection to the database each time,
Disadvantage: It is necessary to waste some memory, occupy some connections,
So if the user visit a large amount of time will be wrong, to the MySQL max_connections parameters, or use mysql_connect () to solve the problem.
Mysqli also has a process-style approach, but it starts with a mysqli prefix, all the same. If Mysqli is manipulated in a procedural way, some functions must specify resources, such as mysqli_query (resource identification, SQL statement), and resource identifier parameters are placed in front, while mysql_query (SQL statement, ' optional ') resource identification is placed behind , and can be unspecified, which defaults to the last open connection or resource.
Instance
Mysqli.dll (object mode):
| The code is as follows |
Copy Code |
$conn = new mysqli (' localhost ', ' user ', ' password ', ' data_base '); The connection here is new, and the last parameter is to specify the database directly, without mysql_select_db (). can also be constructed without specifying, and then $conn-> select_db (' data_base ') $result = $conn-> query (' SELECT * from Data_base '); $row = $result-> fetch_row (); Fetch a row of data Echo Row[0]; Output the value of the first field |
-can be used in the same way as Mysql.dll;
-OO interface support, simple call;
-Support for MYSQL4. new features introduced;
-You can set advanced connection options by using related functions such as mysqli_init ()
General installation when the choice of MySQL, because some servers do not support mysqli.