Php Tutorial: DB class for mysql database operations

Source: Internet
Author: User
Tags pconnect
Php Tutorial: DB class for mysql database operations

Code copy box

/*
* 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 ."
\ R \ n ";
$ Message. = $ this-> get_errno ()."
\ 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" MySQL database error";
Echo" \ R \ n ";
Echo"\ R \ n ";
Echo"
\ R \ n ";
Echo" ". Htmlspecialchars ($ message )."\ R \ n ";
Echo"
\ R \ n";
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.

$ 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.

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.