MySQL Database link code
function Dbconnect ($hostname, $username, $pass, $db _name, $pconnect =0) { $func =empty ($pconnect)? ' Mysql_connect ': ' Mysql_pconnect '; if (! $connect) { [email protected] $func ($hostname, $username, $pass) or Die ("<font size= ' 2 ' >mysql_error:". Mysql_error (). " <br>mysql Error Num: ". Mysql_errno ()." </font> "); } @mysql_select_db ($db _name, $connect) or Die ("<font size= ' 2 ' > Mysql_error:". Mysql_error (). " <br>mysql Error Num: ". Mysql_errno ()." </font> "); return $connect;}
Notes :
Parameter $hostname, $username, $pass, $db _name represents the MySQL database server address, username, password, and the database name of the connection, usually hostname is localhost or 127.0.0.1. The default of parameter $pconnect is 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 : When the current PHP program is executed, PHP automatically shuts down the database connection established by Mysql_connect, and Mysql_ Pconnect returns a persistent and stable database connection, which can be reused for a certain period of time when the next connection request is made, which saves the time of repeatedly connecting to MySQL database, and makes the access speed faster, which is suitable for situations where there is not much concurrent access, such as a large number of concurrent accesses, The maximum number of connections that MySQL has reached may make subsequent requests unsatisfied.
mysql_error function : Returns the text error message generated by the previous MySQL operation. The Mysql_errno function Returns the error number in the last MySQL operation and returns 0 if there are no errors.
MySQL database query code
function Query_error ($query) { global$connect; $temp _bar= "<br>=============================================================================<br>"; $result =mysql_query ($query, $connect) or Die ("DB ERROR <br>". $temp _bar. " <font size= ' 2 ' > Mysql_query: ". $query." <br> mysql_error: ". Mysql_error ()." <br> Mysql Error Num: ". Mysql_errno ()." </font> ". $temp _bar); Return$result;}
Note : This function is a MySQL database query function, equal to the function of the same mysql_query function, if the error output error message (SQL statement), in fact, in order to prevent exposing the structure of the site database, formal commercial, it is best not to output SQL execution statements.
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 ($i =0;num_rows ($result), $i + +) { $info [$i]= @mysql_fetch_array ($result, $result _type); } Free_result ($result); Return$info;} }
Note : The functionality of this function is deferred from the Mysql_fetch_array function, on the basis of which I have added read functionality to the MySQL database recordset and returned the obtained value 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: Gets an array that contains both associative and numeric indexes. Mysql_assoc: Just get the associated index (as in Mysql_fetch_assoc (),mysql_num : Get a 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 "<script language= ' JavaScript ' >alert (' $str '); $url;</script>"; } else{ echo "<script language= ' JavaScript ' > $url;</script>"; } Exit;}
Note : This function mainly in the form of alert error and page jump, is a general function, error or jump before it will be the MySQL database connection closed, using the Mysql_close function.
Invocation Description :
From the function code of the MySQL database operation above, we can see that the $connect variable is a global variable, first put the above several functions into a file, This MySQL database connection function is called after the Dbconnect function declaration, such as mysqlconnect.php, and then after declaring the relevant variable and assigning a value, namely:
$hostname = "Mysqlserveraddr"; $username = "YourUserName"; $pass = "Youruserpass"; $db _name= "yourdatabase"; $connect = Dbconnect ($hostname, $username, $pass, $db _name);
Summarize:
Through the above several MySQL database connection, the database query, the database recordset operation function code explanation, in the PHP website development The MySQL database Operation basic function has included, It is possible to change the MySQL database class based on this code or add other MySQL database operation functions using PHP.
MySQL database connection, query, recordset operation code