PHP4 and MySQL database operation functions (1) _ MySQL

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 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 (databaseserver) functions (2) :( 1 ). 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.


<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:

$ 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:
$ Select = mysql_select_db ('forum ', $ connect );
If ($ select)
{Echo "connect db forum successed! ";}
Else
{Echo "connect db forum failed! ";}
?>

Example 2:
$ 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.

<4>. SQL query functions (2 ):

1. mysql_query ()
Format: int mysql_query (string sqlquery, int link_identifier );
Send a standard SQL statement request to the server. If it fails, a value of False is returned.

Example:
$ Connect = mysql_connect ($ hostname, $ user, $ pwd );
$ Select = mysql_select_db ($ dbname, $ connect );
$ Query = mysql_query ($ SQL, $ connect );
If ($ query) echo "Successed! ";
Else echo "Failed! ";
?>
This function must be used with the mysql_select_db () function. it makes no sense to use it independently!

2. mysql_db_query ()
Format: int mysql_db_query (string database, string sqlquery, int link_identifier );

In this function, you must specify the database name and SQL statement sqlquery. if the query fails, False is returned.

Example:

$ Connect = mysql_connect ($ hostname, $ user, $ pwd );
$ Query = mysql_db_query ($ dbname, $ SQL, $ connect );
If ($ query) echo "Successed! ";
Else echo "Failed! ";
?>

The difference between mysql_db_query () and mysql_query () lies in that the former does not need to use mysql_select_db () to select the database, but selects the database while executing the SQL statement.

Reprint: www.chinaphp.com

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.