PHP implements Mysql database connection, query, record set operation code instance

Source: Internet
Author: User
Mysql database connection, mysql Query, mysql record set operation, PHP technical exchange, PHP blog, PHP achieve mysql database connection, PHP Technical Blog

During PHP website development, you often need to perform operations on the Mysql database. Generally, you need to perform the following steps: Mysql database link, Mysql database query, and Mysql record set operations, if you repeat the above operations each time, it is not only tedious, but also highly redundant code. I have compiled some code that uses PHP to implement Mysql database operations and pasted it as a function, you can add other functions or integrate them into the Mysql database class as needed.

Mysql database link code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Function dbConnect ($ hostname, $ username, $ pass, $ db_name, $ pconnect = 0)
{
$ Func = empty ($ pconnect )? 'MySQL _ connect ': 'MySQL _ pconnect ';

If (! $ Connect ){
$ Connect = @ $ func ($ hostname, $ username, $ pass) or die ("Mysql_Error:". mysql_error ()."
Mysql Error Num: ". mysql_errno ()."");
}

@ Mysql_select_db ($ db_name, $ connect) or die ("Mysql_Error:". mysql_error ()."
Mysql Error Num: ". mysql_errno ()."");

Return $ connect;
}

Note:

The $ hostname, $ username, $ pass, and $ db_name parameters represent the addresses, usernames, passwords, and names of the connected Mysql database servers. Generally, the hostnames are localhost or 127.0.0.1. The default $ pconnect parameter is 0, indicating that the Mysql database is usually connected using the mysql_connect function.

Knowledge Point:

Difference between mysql_connect and mysql_pconnect: after the current PHP program is executed, PHP automatically closes the database connection established by mysql_connect, and mysql_pconnect returns a persistent and stable database connection, when there is a connection request within a certain period of time, it can be reused, saving the time for repeatedly connecting to the Mysql database, making the access speed faster. this is suitable for scenarios with low concurrency traffic, if the concurrency traffic is large, the subsequent requests may not be satisfied because Mysql has reached the maximum number of connections.

Mysql_error function: returns the text error message generated by the previous Mysql operation. The mysql_errno function returns the error number in the previous Mysql operation. If no error occurs, 0 is returned.

Mysql database query code

 

1
2
3
4
5
6
7
8
9
10
11
12
Function query_error ($ query)
{
Global $ connect;
$ Temp_bar ="
========================================================== ============================================
";
$ Result = mysql_query ($ query, $ connect) or die ("DB ERROR
". $ Temp_bar." Mysql_Query: ". $ query ."
Mysql_Error: ". mysql_error ()."
Mysql Error Num: ". mysql_errno ()." ". $ temp_bar );
Return $ result;
}

Note: This function is a Mysql database query function, which is equivalent to the function of mysql_query. If an error occurs, an error message (SQL statement) is output. to prevent exposing the structure of the website database, it is recommended that you do not output SQL execution statements for commercial use.

Mysql record set operation function code (mysql_fetch_array)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Function fetch_array ($ result, $ result_type = MYSQL_ASSOC, $ records = "one ")
{
If ($ records = "one "){
Return @ mysql_fetch_array ($ result, $ result_type );
}
Else {
For ($ I = 0; num_rows ($ result); $ I ++)
{
$ Info [$ I] = @ mysql_fetch_array ($ result, $ result_type );
}

Free_result ($ result );

Return $ info;
}
}

Note: The function of this function is extended by the mysql_fetch_array function. On this basis, I added the read function for the Mysql database record set and returned the obtained value in the form of an array.

Knowledge Point:

The mysql_fetch_array function is an extension of the mysql_fetch_row function. The second result_type parameter has three values: MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH. The default value is MYSQL_BOTH. MYSQL_BOTH: get an array that contains both join and numeric indexes. MYSQL_ASSOC: only get the associated index (like mysql_fetch_assoc (), MYSQL_NUM: get the digital index (like mysql_fetch_row ).

Error message function code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Function error_msg ($ msg, $ url = "")
{
Global $ connect;

If ($ connect ){
Mysql_close ($ connect );
}

Switch ($ url)
{
Case "":
$ Url = "history. go (-1 )";
Break;
Case "close ":
$ Url = "window. close ()";
Break;
Default:
$ Url = "document. location. href = '$ url '";
Break;
}

If (! Empty ($ msg )){
Echo"

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.