PHP4 and MySQL database operation function detailed explanation (i)

Source: Internet
Author: User
Tags format execution connect mysql mysql database port number

Say PHP can not mention MySQL, but to talk about MySQL, then PHP is bound to be mentioned. The rapid rise of PHP can not be separated from MySQL, and MySQL is widely used, but also closely related to PHP.

The following is a detailed analysis of PHP4 in the MySQL-related operations function (a total of 32, beginning with Mysql_):

<1>. Functions to connect to the database server (2):

(1). mysql_connect ()
Format: int mysql_connect (string [hostname] [:p ort],string [username],string [password]);

The port parameter in the parameter represents the port number of the database server, usually with its default port number.
If you do not fill in any parameters, the default hostname is localhost,username to root,password null.

function succeeded, returned an int type connection number (Link_identifier), failed, and returned a value of false.

Example:

<?php

$connect = mysql_connect ("localhost", "User", "password");
if ($connect) echo "Connect successed!"; Connection successful, show connect successed!
else echo "Connect failed!"; Connection failed, show connect failed!



?>

In the example above, if mysql_connect () fails, the system's error prompts are displayed, and then the execution continues. So, how do you block these system error prompts and end the program after the failure?
In MySQL, allow the database function to be preceded by the @ symbol, shielding the system's error prompts, with the Die () function to give more understandable error prompts, and then the Die () function will automatically exit the program.

The previous example could read:

<?php

$connect = @mysql_connect ("localhost", "User", "password") or Die ("Unable to connect database server!");

?>

If the mysql_connect () execution fails, the Unable to connect database server! will be displayed after exiting the program.

(2). Mysql_pconnect ()
Format: int mysql_pconnect (string [hostname] [:p ort],string [username],string [password]);
This function is essentially the same as the mysql_connect () of (1), except that:

---------when the database operation is complete, the connection established by (1) mysql_connect () is automatically closed, and (2) the connection established by Mysql_pconnect () will continue to exist, a solid and persistent connection.
---------in (2) mysql_pconnect (), before each connection, will check whether there is a connection using the same hostname,use,password, if so, use this connection number directly.
The connection established by the mysql_connect () of the---------(1) can be closed with mysql_close (), and (2) mysql_pconnect () cannot be closed with mysql_close ().


&LT;2&GT; Close database connection functions (1):

Mysql_close ()
Format: int mysql_close (int link_identifier);
Closes the connection established by the mysql_connect () function, performs successfully, returns a Ture value, and returns a value of false for failure.

Examples are as follows:
<?php

$connect = @mysql_connect ("hostname", "User", "password") or Die ("Unable to connect database server!");

$close = @mysql_close ($connect) or Die ("Unable to close database server connect!");

?>

Note: mysql_close () cannot close the connection established by the Mysql_pconnect () function.



Related Article

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.