PHP4 and MySQL database control functions

Source: Internet
Author: User
MySQL cannot be mentioned in PHP, But MySQL must be mentioned in PHP. The rapid rise of PHP is inseparable from MySQL, and the widespread use of MySQL is also related to PHP. The following is a detailed analysis of the functions related to MySQL in PHP4 (32 functions in total, PHP cannot be mentioned without MySQL. to talk about MySQL, PHP must also be mentioned. The rapid rise of PHP is inseparable from MySQL, and the widespread use of MySQL is also related to PHP.
  
The following is a detailed analysis of the functions related to MySQL in PHP4 (a total of 32 functions starting with mysql _):
  
<1>. database server connection functions (2 ):
  
(1). mysql_connect ()
Pattern: int mysql_connect (string [hostname] [: port], string [username], string [password]);
  
The port parameter in the parameter represents the port number of the database server. Generally, 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. Connect Failed is displayed!
  
  
  
?>
  
In the preceding example, if mysql_connect () fails to be executed, a system error message is displayed, and the subsequent execution continues. Then, how can we block the system error prompts and end the program after the failure?
In MySQL, I promise to add the @ symbol before the database function to block system error prompts. I also use the die () function to give more understandable error prompts, and then die () the function will automatically exit 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 ()
Pattern: int mysql_pconnect (string [hostname] [: port], string [username], string [password]);
This function is similar to (1) mysql_connect (). The difference is:
  
--------- After the database is stopped, connections established by (1) mysql_connect () will be closed, while connections established by (2) mysql_pconnect () will continue to exist, is a solid persistent connection.
--------- In (2) mysql_pconnect (), before each connection, the system will check whether the same hostname, use, and password connections are applied. If yes, the connection number is directly applied.
The connection established by mysql_connect () of --------- (1) can be closed by mysql_close (), while mysql_pconnect () of (2) cannot be closed by mysql_close.
  
  
<2>. closed database connection function (1 ):
  
Mysql_close ()
Pattern: int mysql_close (int link_identifier );
Closes the connection established by the mysql_connect () function. if the connection is successful, the true value is returned. if the connection fails, the false value is returned.
  
Example:

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.