Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to operate Mysql database connections, queries, recordsets, etc., using PHP

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

The following mainly brings you how to use PHP to operate Mysql database connections, queries, recordsets, and so on. I hope that how to use PHP to operate Mysql database connections, queries, recordsets and so on can bring you practical use, which is also the main purpose of my editing this article. All right, don't talk too much nonsense, let's just read the following.

Mysql database link code

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 parameter $hostname,$username,$pass,$db_name represents the Mysql database CVM address, user name, password, and database name of the connection, respectively. Usually, hostname is localhost or 127.0.0.1. The parameter $pconnect defaults to 0, which means that the Mysql database is usually connected with the mysql_connect function.

Knowledge points:

The difference between mysql_connect and mysql_pconnect: after the execution of the current PHP program, PHP automatically closes the database connection established by mysql_connect, while mysql_pconnect returns a persistent database connection, which can be reused when there is the next connection request within a certain period of time, which saves the time of repeatedly connecting to the Mysql database and speeds up the access speed. It is suitable for situations where the amount of concurrent visits is small, such as a large amount of concurrent visits. Subsequent requests may not be satisfied because the 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 from the previous Mysql operation, or 0 if there is no error.

Mysql database query code

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 equal to the function of the mysql_query function. If something goes wrong, it will output error information (SQL statement). In fact, in order to prevent the structure of the website database from being exposed, it is best not to output SQL execution statements when officially commercial.

Mysql Recordset Operation function Code (mysql_fetch_array)

Function fetch_array ($result,$result_type = MYSQL_ASSOC,$records = "one") {if ($records = = "one") {return @ mysql_fetch_array ($result,$result_type);} else {for ($info [$I] = @ mysql_fetch_array ($result,$result_type);} free_result ($result); return $info;}}

Note: the function of this function is derived from the mysql_fetch_array function. On this basis, I added the ability to read the Mysql database recordset and return the obtained values as an array.

Knowledge points:

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

Error message function code

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 "alert ('$str'); $url;";} else {echo "$url;";} exit;}

Note: the function of this function is mainly in the form of alert to report errors and jump pages. It is a general function that closes the Mysql database connection before reporting errors or redirects, using the mysql_close function.

Call description:

From the function code of the above Mysql database operation, we can see that the $connect variable is a global variable. First, put the above functions into a file, such as mysqlconnect.php, then declare the relevant variables and assign values, and call the Mysql database connection function after the dbConnect function is declared, that is:

$hostname = "mysqlserveraddr"; $username = "yourusername"; $pass = "youruserpass"; $db_name = "yourdatabase"; $connect = dbConnect ($hostname,$username,$pass,$db_name)

For the above about how to use PHP to operate Mysql database connections, queries, recordsets, etc., we do not find it very helpful. If you need to know more, please continue to follow our industry information. I'm sure you'll like it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report