MySQL cannot be mentioned in PHP, But MySQL must be mentioned in PHP. The rapid rise of PHP is inseparable from MySQL, and the wide application of MySQL is also related to PHP.
The following is a detailed analysis of MySQL-related functions in PHP4 (a total of 32 functions starting with mysql _):
<1>. database server connection functions (2 ):
(1). mysql_connect ()
Format: int mysql_connect (string [hostname] [ort], string [username], string [password]);
The port parameter in the parameter indicates the port number of the database server. You can use its default port number.
If no parameter is specified, the default hostname is localhost, username is root, and password is empty.
If the function is successfully executed, a connection number (link_identifier) of the int type is returned. If the function fails to be executed, a value of false is returned.
Example:
$ Connect = mysql_connect ("localhost", "user", "password ");
If ($ connect) echo "Connect Successed! "; // The connection is successful. Connect Successed is displayed!
Else echo "Connect Failed! "; // Connection Failed, show Connect Failed!
?>
In the preceding example, if mysql_connect () fails to be executed, a system error message is displayed and the execution continues. Then, how can we block the system error prompts and end the program after the failure?
In MySQL, the @ symbol can be added before the database function to block system error prompts. At the same time, the die () function is used to provide more understandable error prompts, and then die () the function automatically exits the program.
The preceding example can be changed:
$ Connect = @ mysql_connect ("localhost", "user", "password") or die ("Unable to connect database server! ");
?>
If mysql_connect () fails to be executed, Unable to connect database server will be displayed! And then exit the program.
(2). mysql_pconnect ()
Format: int mysql_pconnect (string [hostname] [ort], string [username], string [password]);
This function is basically the same as mysql_connect () in (1). The difference is:
--------- After the database operation is complete, the connection established by (1) mysql_connect () will be closed automatically, and the connection established by (2) mysql_pconnect () will continue to exist, is a stable and persistent connection.
--------- In mysql_pconnect () of (2), before each connection, the system checks whether the same hostname, use, and password connections are used. If yes, the connection number is used directly.
The connection established by mysql_connect () of --------- (1) can be closed with mysql_close (), while mysql_pconnect () of (2) cannot be closed with mysql_close.
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.