CopyCode The Code is as follows: <? PHP
/*
* MySQL database DB
* @ Package DB
* @ Author yytcpt (Shadow)
* @ Version 2008-03-27
* @ Copyrigth http://www.d5s.cn/
*/
Class dB {
VaR $ connection_id = "";
VaR $ pconnect = 0;
VaR $ shutdown_queries = array ();
VaR $ queries = array ();
VaR $ query_id = "";
VaR $ query_count = 0;
VaR $ record_row = array ();
VaR $ failed = 0;
VaR $ halt = "";
VaR $ query_log = array ();
Function connect ($ db_config ){
If ($ this-> pconnect ){
$ This-> connection_id = mysql_pconnect ($ db_config ["hostname"], $ db_config ["username"], $ db_config ["password"]);
} Else {
$ This-> connection_id = mysql_connect ($ db_config ["hostname"], $ db_config ["username"], $ db_config ["password"]);
}
If (! $ This-> connection_id ){
$ This-> halt ("can not connect MySQL Server ");
}
If (! @ Mysql_select_db ($ db_config ["Database"], $ this-> connection_id )){
$ This-> halt ("can not connect MySQL Database ");
}
If ($ db_config ["charset"]) {
@ Mysql_unbuffered_query ("set names '". $ db_config ["charset"]. "'");
}
Return true;
}
// Send an SQL query and return the result set
Function query ($ query_id, $ query_type = 'mysql _ query '){
$ This-> query_id = $ query_type ($ query_id, $ this-> connection_id );
$ This-> queries [] = $ query_id;
If (! $ This-> query_id ){
$ This-> halt ("query failed: \ N $ query_id ");
}
$ This-> query_count ++;
$ This-> query_log [] = $ STR;
Return $ this-> query_id;
}
// The row that sends an SQL query and does not obtain or cache the result.
Function query_unbuffered ($ SQL = ""){
Return $ this-> query ($ SQL, 'mysql _ unbuffered_query ');
}
// Obtain a row from the result set as an associated array
Function fetch_array ($ SQL = ""){
If ($ SQL = "") $ SQL = $ this-> query_id;
$ This-> record_row = @ mysql_fetch_array ($ SQL, mysql_assoc );
Return $ this-> record_row;
}
Function shutdown_query ($ query_id = ""){
$ This-> shutdown_queries [] = $ query_id;
}
// Obtain the number of rows in the result set. Only insert, update, or delete rows are returned.
Function affected_rows (){
Return @ mysql_affected_rows ($ this-> connection_id );
}
// Obtain the number of rows in the result set, which is only valid for select statements.
Function num_rows ($ query_id = ""){
If ($ query_id = "") $ query_id = $ this-> query_id;
Return @ mysql_num_rows ($ query_id );
}
// Returns the numeric encoding of the error message in the previous MySQL operation.
Function get_errno (){
$ This-> errno = @ mysql_errno ($ this-> connection_id );
Return $ this-> errno;
}
// Obtain the ID generated by the previous insert operation
Function insert_id (){
Return @ mysql_insert_id ($ this-> connection_id );
}
// Obtain the number of queries
Function query_count (){
Return $ this-> query_count;
}
// Release the result memory
Function free_result ($ query_id = ""){
If ($ query_id = "") $ query_id = $ this-> query_id;
@ Mysql_free_result ($ query_id );
}
// Close the MySQL connection
Function close_db (){
If ($ this-> connection_id) return @ mysql_close ($ this-> connection_id );
}
// List tables in the MySQL database
Function get_table_names (){
Global $ db_config;
$ Result = mysql_list_tables ($ db_config ["Database"]);
$ Num_tables = @ mysql_numrows ($ result );
For ($ I = 0; $ I <$ num_tables; $ I ++ ){
$ Tables [] = mysql_tablename ($ result, $ I );
}
Mysql_free_result ($ result );
Return $ tables;
}
// Retrieve the column information from the result set and return it as an object to retrieve all fields
Function get_result_fields ($ query_id = ""){
If ($ query_id = "") $ query_id = $ this-> query_id;
While ($ field = mysql_fetch_field ($ query_id )){
$ Fields [] = $ field;
}
Return $ fields;
}
// Error message
Function halt ($ the_error = ""){
$ Message = $ the_error. "<br/> \ r \ n ";
$ Message. = $ this-> get_errno (). "<br/> \ r \ n ";
$ SQL = "insert into 'db _ error' (pagename, errstr, timer) values ('". $ _ server ["php_self"]. "','". addslashes ($ message ). "',". time (). ")";
@ Mysql_unbuffered_query ($ SQL );
If (DEBUG = true ){
Echo "<HTML> Echo "<style type = \" text/CSS \ "> <! --. Error {Font: 11px tahoma, verdana, Arial, sans-serif, simsun ;}--> </style> Echo "<body> \ r \ n ";
Echo "<BLOCKQUOTE> \ r \ n ";
Echo "<textarea class = \" error \ "rows = \" 15 \ "Cols = \" 100 \ "Wrap = \" on \ "> ". htmlspecialchars ($ message ). "</textarea> \ r \ n ";
Echo "</BLOCKQUOTE> \ r \ n </body> Exit;
}
}
Function _ destruct (){
$ This-> shutdown_queries = array ();
$ This-> close_db ();
}
Function SQL _select ($ tbname, $ where = "", $ Limit = 0, $ fields = "*", $ orderby = "ID", $ sort = "DESC "){
$ SQL = "select". $ fields. "From '". $ tbname. "'". ($ where? "Where". $ where: ""). "order by". $ orderby. "". $ sort. ($ limit? "Limit". $ limit :"");
Return $ SQL;
}
Function SQL _insert ($ tbname, $ row ){
Foreach ($ row as $ key => $ value ){
$ Sqlfield. = $ key .",";
$ Sqlvalue. = "'". $ value ."',";
}
Return "insert '". $ tbname. "'(". substr ($ sqlfield, 0,-1 ). ") values (". substr ($ sqlvalue, 0,-1 ). ")";
}
Function SQL _update ($ tbname, $ row, $ where ){
Foreach ($ row as $ key => $ value ){
$ Sqlud. = $ key. "= '". $ value ."',";
}
Return "updat'". $ tbname. "'set". substr ($ sqlud, 0,-1). "Where". $ where;
}
Function SQL _delete ($ tbname, $ where ){
Return "delete from '". $ tbname. "'where". $ where;
}
// Add a new record
Function row_insert ($ tbname, $ row ){
$ SQL = $ this-> SQL _insert ($ tbname, $ row );
Return $ this-> query_unbuffered ($ SQL );
}
// Update a specified record
Function row_update ($ tbname, $ row, $ where ){
$ SQL = $ this-> SQL _update ($ tbname, $ row, $ where );
Return $ this-> query_unbuffered ($ SQL );
}
// Delete records that meet the conditions
Function row_delete ($ tbname, $ where ){
$ SQL = $ this-> SQL _delete ($ tbname, $ where );
Return $ this-> query_unbuffered ($ SQL );
}
/* All records are returned Based on the condition query.
* $ Tbname table name, $ where query condition, $ limit return record, $ fields return Field
*/
Function row_select ($ tbname, $ where = "", $ Limit = 0, $ fields = "*", $ orderby = "ID", $ sort = "DESC "){
$ SQL = $ this-> SQL _select ($ tbname, $ where, $ limit, $ fields, $ orderby, $ sort );
Return $ this-> row_query ($ SQL );
}
// Display a record in detail
Function row_select_one ($ tbname, $ where, $ fields = "*", $ orderby = "ID "){
$ SQL = $ this-> SQL _select ($ tbname, $ where, 1, $ fields, $ orderby );
Return $ this-> row_query_one ($ SQL );
}
Function row_query ($ SQL ){
$ Rs = $ this-> query ($ SQL );
$ Rs_num = $ this-> num_rows ($ RS );
$ Rows = array ();
For ($ I = 0; $ I <$ rs_num; $ I ++ ){
$ Rows [] = $ this-> fetch_array ($ RS );
}
$ This-> free_result ($ RS );
Return $ rows;
}
Function row_query_one ($ SQL ){
$ Rs = $ this-> query ($ SQL );
$ ROW = $ this-> fetch_array ($ RS );
$ This-> free_result ($ RS );
Return $ row;
}
// Counting statistics
Function row_count ($ tbname, $ where = ""){
$ SQL = "select count (ID) as row_sum from '". $ tbname. "'". ($ where? "Where". $ where :"");
$ ROW = $ this-> row_query_one ($ SQL );
Return $ row ["row_sum"];
}
}
?>
I haven't posted a post for a long time. I will share some of my frequently used PHP files.
If you have added new features or made improvements, please share with us.Copy codeThe Code is as follows: <? PHP
$ Db_config ["hostname"] = "127.0.0.1"; // server address
$ Db_config ["username"] = "root"; // database username
$ Db_config ["password"] = "root"; // Database Password
$ Db_config ["Database"] = "wap_blueidea_com"; // Database Name
$ Db_config ["charset"] = "utf8 ";
Include ('db. php ');
$ Db = new dB ();
$ Db-> connect ($ db_config );
// Example: Query all records with cid = 1 in table_name.
$ ROW = $ db-> row_select ('table _ name', 'cid = 1 ');
?>
For more detailed usage, see note in the DB class file.