PHP MySQL Functions

Source: Internet
Author: User
Tags mysql functions php mysql
The following describes the functions related to MySQL operations in PhP4 (start with MySQL _):

<1>. database server connection functions (2 ):

(1). mysql_connect ()

Format: int mysql_connect (string [hostname] [: Port], 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:

<? PHP
$ 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 should we block the error prompts of these systems and end after the failure?Program?
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:
<? PHP
$ 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] [: Port], 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.
<2>. Disable the database connection function (1 ):

Mysql_close ()

Format: int mysql_close (INT link_identifier );

Close the connection established by the mysql_connect () function. If the connection runs successfully, the true value is returned. If the connection fails, the false value is returned.

Example:
<? 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.

<3>. Select database functions (1 ):

Mysql_select_db ()
Format: int mysql_select_db (string Database Name, int link_identifier );

Select the specified database name. If the database name is successful, one true value is returned. If the database name fails, one false value is returned.

Example 1:
<? PHP
$ Select = mysql_select_db ('Forum ', $ Connect );
If ($ select)
{Echo "Connect dB Forum successed! ";}
Else
{Echo "Connect dB Forum failed! ";}
?>

Example 2:
<? PHP
$ Select = mysql_select_db ("Forum", $ Connect) or die ("can not connect this dB! ");
?>

Note: This function is equivalent to the use statement in MySQL, for example, use forum.

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.