Very complete PHP mysql class

Source: Internet
Author: User
& Amp; nbsp; very complete PHP MySQL operation class, even if PDO, ActiveRecord, ORM, Framework, Framework... It is not as powerful and easy to use as it is, and you won't

Very complete PHP MySQL operation class, even if PDO, ActiveRecord, ORM, Framework, Framework... Not as powerful and easy to use. with it, you will no longer need any MySQL encapsulation. This type has been widely used in many large projects and has been tested in the production environment for at least two years.

Let's take a look at this class now!

 

Class Mysql {
Var $ conn;
Var $ query_list = array ();
Public $ query_count = 0;

Public function _ construct ($ c ){
If (! Isset ($ c ['port']) {
$ C ['port'] = '123 ';
}
$ Server = $ c ['host']. ':'. $ c ['port'];
$ This-> conn = mysql_connect ($ server, $ c ['username'], $ c ['password'], true) or die ('connect db error ');
Mysql_select_db ($ c ['dbname'], $ this-> conn) or die ('select db error ');
If ($ c ['charset']) {
Mysql_query ("set names". $ c ['charset'], $ this-> conn );
}
}

/**
* Execute mysql_query and return the result.
*/
Public function query ($ SQL ){
$ Stime = microtime (true );

$ Result = mysql_query ($ SQL, $ this-> conn );
$ This-> query_count ++;
If ($ result = false ){
Throw new Exception (mysql_error ($ this-> conn). "in SQL: $ SQL ");
}

$ Etime = microtime (true );
$ Time = number_format ($ etime-$ stime) * 1000, 2 );
$ This-> query_list [] = $ time. ''. $ SQL;
Return $ result;
}

/**
* Execute an SQL statement and return the first record of the result (an object ).
*/
Public function get ($ SQL ){
$ Result = $ this-> query ($ SQL );
If ($ row = mysql_fetch_object ($ result )){
Return $ row;
} Else {
Return null;
}
}

/**
* Return the query result set, which is organized by key into an associated array. each element is an object.
* If the key is empty, the result is organized into a normal array.
*/
Public function find ($ SQL, $ key = null ){
$ Data = array ();
$ Result = $ this-> query ($ SQL );
While ($ row = mysql_fetch_object ($ result )){
If (! Empty ($ key )){
$ Data [$ row-> {$ key}] = $ row;
} Else {
$ Data [] = $ row;
}
}
Return $ data;
}

Public function last_insert_id (){
Return mysql_insert_id ($ this-> conn );
}

/**
* Execute a count SQL statement with the result set count and return the count.
*/
Public function count ($ SQL ){
$ Result = $ this-> query ($ SQL );
If ($ row = mysql_fetch_array ($ result )){
Return (int) $ row [0];
} Else {
Return 0;
}
}

/**
* Start a transaction.
*/
Public function begin (){
Mysql_query ('begin ');
}

/**
* Submit a transaction.
*/
Public function commit (){
Mysql_query ('commit ');
}

/**
* Roll back a transaction.
*/
Public function rollback (){
Mysql_query ('rollback ');
}

/**
* Obtain the record of the specified number.
* @ Param int $ id the number of the record to be retrieved.
* @ Param string $ field name. The default value is 'id '.
*/
Function load ($ table, $ id, $ field = 'id '){
$ SQL = "select * from '{$ table} 'where' {$ field}' = '{$ id }'";
$ Row = $ this-> get ($ SQL );
Return $ row;
}

/**
* Save a record. after the call, the id is set.
* @ Param object $ row
*/
Function save ($ table, & $ row ){
$ SqlA = '';
Foreach ($ row as $ k => $ v ){
$ SqlA. = "'$ k' =' $ V ',";
}

$ SqlA = substr ($ sqlA, 0, strlen ($ sqlA)-1 );
$ SQL = "insert into '{$ table}' set $ sqlA ";
$ This-> query ($ SQL );
If (is_object ($ row )){
$ Row-> id = $ this-> last_insert_id ();
} Else if (is_array ($ row )){
$ Row ['id'] = $ this-> last_insert_id ();
}
}

/**
* Update the record specified by $ arr [id.
* @ Param array $ row indicates the record to be updated. The value of the array item with the key name id indicates the record to be updated.
* @ Return int indicates the number of rows affected.
* @ Param string $ field name. The default value is 'id '.
*/
Function update ($ table, & $ row, $ field = 'id '){
$ SqlA = '';
Foreach ($ row as $ k => $ v ){
$ SqlA. = "'$ k' =' $ V ',";
}

$ SqlA = substr ($ sqlA, 0, strlen ($ sqlA)-1 );
If (is_object ($ row )){
$ Id = $ row-> {$ field };
} Else if (is_array ($ row )){
$ Id = $ row [$ field];
}
$ SQL = "update' {$ table} 'set $ sqlA where' {$ field} '=' $ ID '";
Return $ this-> query ($ SQL );
}

/**
* Delete a record.
* @ Param int $ id the number of the record to be deleted.
* @ Return int indicates the number of rows affected.
* @ Param string $ field name. The default value is 'id '.
*/
Function remove ($ table, $ id, $ field = 'id '){
$ SQL = "delete from '{$ table} 'where' {$ field}' = '{$ id }'";
Return $ this-> query ($ SQL );
}

Function escape (& $ val ){
If (is_object ($ val) | is_array ($ val )){
$ This-> escape_row ($ val );
}
}

Function escape_row (& $ row ){
If (is_object ($ row )){
Foreach ($ row as $ k => $ v ){
$ Row-> $ k = mysql_real_escape_string ($ v );
}
} Else if (is_array ($ row )){
Foreach ($ row as $ k => $ v ){
$ Row [$ k] = mysql_real_escape_string ($ v );
}
}
}

Function escape_like_string ($ str ){
$ Find = array ('% ','_');
$ Replace = array ('\ % ','\_');
$ Str = str_replace ($ find, $ replace, $ str );
Return $ str;
}
}
?>

Example:


 Save ('Table _ 1', $ row); // update $ db-> update ('Table _ 1', $ row ); // delete $ db-> remove ('Table _ 1', 1); // query $ rows = $ db-> find ($ SQL, 'id')?>
Related Article

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.