Mysql operations written in php

Source: Internet
Author: User

<? Php

Class Db {

Private $ con;
 
 
Function _ construct ($ mysql_data ){
 
$ This-> db_connect ($ mysql_data );
 
}
/**
* Connect to the database
* @ Param array $ mysql_data database connection information array ('host' => 'host: port', 'user' => 'user', 'pass' => 'Password ', 'dbbase' => 'database', 'charset' => 'character type ')
**/
Function db_connect ($ mysql_data ){
 
$ Host = $ mysql_data ['host'];
$ User = $ mysql_data ['user'];
$ Pass = $ mysql_data ['pass'];
$ Dbbase = $ mysql_data ['dbbase'];
$ Charset = $ mysql_data ['charset'];
 
$ This-> con = @ mysql_connect ($ host, $ user, $ pass) or die ('no Server ');
If ($ this-> con ){
Mysql_select_db ($ dbbase, $ this-> con) or die ('no DATABASE: '. $ dbbase );

Mysql_query ('set names'. $ charset );
}
}
 
 
/**
* Execute SQL statements
* @ Param string $ the SQL statement to be executed
* @ Return obj
**/
Function query ($ SQL ){
 
Return mysql_query ($ SQL, $ this-> con );
 
}

 
/**
* Obtain a query result.
* @ Param string $ the SQL statement to be executed
* @ Return array ('id' => '1', 'dd' => '2 ');
**/
Function fetch_row ($ SQL ){
 
$ Result = $ this-> query ($ SQL );
 
Return mysql_fetch_assoc ($ result );
}
 
/**
* Obtain the first field value of a result.
* @ Param string $ the SQL statement to be executed
* @ Return string
**/
Function fetch_first ($ SQL ){
 
$ Result = $ this-> query ($ SQL );
 
$ Result = mysql_fetch_array ($ result );
 
Return $ result [0];
}
 
 
/**
* Returned result set
* @ Param string $ the SQL statement to be executed
* @ Param string $ filed the field to be indexed
* @ Return array ('id' => '1', 'dd' => '2'), array ('id' => '1 ', 'dd' => '2 '));
**/
Function fetch_result ($ SQL, $ field = null ){
 
$ Result = $ this-> query ($ SQL );
$ Ret_array = array ();
 
While ($ rows = mysql_fetch_assoc ($ result )){

If ($ field ){
$ Ret_array [$ rows [$ field] = $ rows;
} Else {
$ Ret_array [] = $ rows;
}

}
 
Return $ ret_array;
}
 
/**
* Obtain the new ID.
* @ Return int
**/
Function get_insertid (){
Return mysql_insert_id ();
}
 
/**
* Insert a single data table
* @ Param array $ Data array ('field 1' => 'value', 'field 2' => 'value ',)
* @ Param string $ table Name
**/
Function insert ($ Data, $ table ){
 
// Insert into table (field, field) values (value, value)
 
$ Key_array = implode (',', array_keys ($ Data ));
$ Key_val = '\ ''. implode (' \ ', \'', array_values ($ Data )).'\'';
 
$ SQL = "insert into". $ table. "($ key_array) values ($ key_val )";
 
Return $ this-> query ($ SQL );
}
 
 
 
/**
* Update a single table
* @ Param array $ Data array ('field 1' => 'value', 'field 2' => 'value ',)
* @ Param array $ where array ('field 1' => 'value', 'field 2' => 'value') Conditions to be updated
* @ Param string $ table name www.2cto.com
**/
Function update ($ Data, $ table, $ where ){
// Update table set field = value, field = value where key = value;
$ Key_var = array ();
 
Foreach ($ Data as $ key => $ val ){

$ Key_var [] = $ key. "= '". $ val ."'";
}
 
$ Key_var = implode (',', $ key_var );
 
 
$ Whe_var = array ();
 
Foreach ($ where as $ key => $ val ){

$ Whe_var [] = $ key. "= '". $ val ."'";
}
 
$ Whe_var = implode ('and', $ whe_var );
 
If ($ whe_var ){
$ Whe_var = 'where'. $ whe_var;
}
 
$ SQL = "update". $ table. "set". $ key_var. $ whe_var;
Return $ this-> query ($ SQL );
// Return $ SQL;

}
}


From adamboy

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.