PHP database operations Class/Class-PHP source code

Source: Internet
Author: User
Tags pconnect php database
The following is a database operation code commonly used in self-developed applications. There are two files below: configuration files and operation database files, finally, we briefly list the call methods of this example class. The following is a database operation code commonly used in self-developed applications. There are two files below: configuration files and operation database files, finally, we briefly list the call methods of this example class.

Script ec (2); script

Configuration file config. db. php

The Code is as follows:


$ Db_config ["hostname"] = "localhost"; // server address
$ Db_config ["username"] = "root"; // database username
$ Db_config ["password"] = "123"; // Database password
$ Db_config ["database"] = "test"; // database Name
$ Db_config ["charset"] = "utf8"; // database Encoding
$ Db_config ["pconnect"] = 1; // enable persistent connection
$ Db_config ["log"] = 1; // enable log
$ Db_config ["logfilepath"] = './'; // Enable Logging
?>

Database Operations

The Code is as follows:

Time = $ this-> microtime_float ();
Require_once ("config. db. php ");
$ This-> connect ($ db_config ["hostname"], $ db_config ["username"], $ db_config ["password"], $ db_config ["database"], $ db_config ["pconnect"]);
$ This-> is_log = $ db_config ["log"];
If ($ this-> is_log ){
$ Handle = fopen ($ db_config ["logfilepath"]. "dblog.txt", "a + ");
$ This-> handle = $ handle;
}
}

// Database connection
Public function connect ($ dbhost, $ dbuser, $ dbpw, $ dbname, $ pconnect = 0, $ charset = 'utf8 '){
If ($ pconnect = 0 ){
$ This-> link_id = @ mysql_connect ($ dbhost, $ dbuser, $ dbpw, true );
If (! $ This-> link_id ){
$ This-> halt ("database connection failed ");
}
} Else {
$ This-> link_id = @ mysql_pconnect ($ dbhost, $ dbuser, $ dbpw );
If (! $ This-> link_id ){
$ This-> halt ("database persistent connection failed ");
}
}
If (select_db ($ dbname, $ this-% 3Elink_id ">! @ Mysql_select_db ($ dbname, $ this-> link_id )){
$ This-> halt ('database selection failed ');
}
@ Mysql_query ("set names". $ charset );
}

// Query
Public function query ($ SQL ){
$ This-> write_log ("query". $ SQL );
$ Query = mysql_query ($ SQL, $ this-> link_id );
If (! $ Query) $ this-> halt ('query Error: '. $ SQL );
Return $ query;
}

// Obtain a record (MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH)
Public function get_one ($ SQL, $ result_type = MYSQL_ASSOC ){
$ Query = $ this-> query ($ SQL );
$ Rt = & mysql_fetch_array ($ query, $ result_type );
$ This-> write_log ("get a record". $ SQL );
Return $ rt;
}

// Obtain all records
Public function get_all ($ SQL, $ result_type = MYSQL_ASSOC ){
$ Query = $ this-> query ($ SQL );
$ I = 0;
$ Rt = array ();
While ($ row = & mysql_fetch_array ($ query, $ result_type )){
$ Rt [$ I] = $ row;
$ I ++;
}
$ This-> write_log ("retrieve all records". $ SQL );
Return $ rt;
}

// Insert
Public function insert ($ table, $ dataArray ){
$ Field = "";
$ Value = "";
If (! Is_array ($ dataArray) | count ($ dataArray) <= 0 ){
$ This-> halt ('no data to be inserted ');
Return false;
}
While (list ($ key, $ val) = each ($ dataArray )){
$ Field. = "$ key ,";
$ Value. = "'$ val ',";
}
$ Field = substr ($ field, 0,-1 );
$ Value = substr ($ value, 0,-1 );
$ SQL = "insert into $ table ($ field) values ($ value )";
$ This-> write_log ("insert". $ SQL );
If (! $ This-> query ($ SQL) return false;
Return true;
}

// Update
Public function update ($ table, $ dataArray, $ condition = ""){
If (! Is_array ($ dataArray) | count ($ dataArray) <= 0 ){
$ This-> halt ('no data to Update ');
Return false;
}
$ Value = "";
While (list ($ key, $ val) = each ($ dataArray ))
$ Value. = "$ key = '$ val ',";
$ Value. = substr ($ value, 0,-1 );
$ SQL = "update $ table set $ value where 1 = 1 and $ condition ";
$ This-> write_log ("Update". $ SQL );
If (! $ This-> query ($ SQL) return false;
Return true;
}

// Delete
Public function delete ($ table, $ condition = ""){
If (empty ($ condition )){
$ This-> halt ('the condition for deletion not set ');
Return false;
}
$ SQL = "delete from $ table where 1 = 1 and $ condition ";
$ This-> write_log ("delete". $ SQL );
If (! $ This-> query ($ SQL) return false;
Return true;
}

// Return result set
Public function fetch_array ($ query, $ result_type = MYSQL_ASSOC ){
$ This-> write_log ("returned result set ");
Return mysql_fetch_array ($ query, $ result_type );
}

// Obtain the number of records
Public function num_rows ($ results ){
If (! Is_bool ($ results )){
$ Num = mysql_num_rows ($ results );
$ This-> write_log ("the number of retrieved records is". $ num );
Return $ num;
} Else {
Return 0;
}
}

// Release the result set
Public function free_result (){
$ Void = func_get_args ();
Foreach ($ void as $ query ){
If (is_resource ($ query) & get_resource_type ($ query) === 'mysql result '){
Return mysql_free_result ($ query );
}
}
$ This-> write_log ("Release result set ");
}

// Obtain the last inserted id
Public function insert_id (){
$ Id = mysql_insert_id ($ this-> link_id );
$ This-> write_log ("the last inserted id is". $ id );
Return $ id;
}

// Close the database connection
Protected function close (){
$ This-> write_log ("Database Connection closed ");
Return @ mysql_close ($ this-> link_id );
}

// Error message
Private function halt ($ msg = ''){
$ Msg. = "\ r \ n". mysql_error ();
$ This-> write_log ($ msg );
Die ($ msg );
}

// Destructor
Public function _ destruct (){
$ This-> free_result ();
$ Use_time = ($ this-> microtime_float ()-($ this-> time );
$ This-> write_log ("the time used to complete the entire Query Task is". $ use_time );
If ($ this-> is_log ){
Fclose ($ this-> handle );
}
}

// Write the log file
Public function write_log ($ msg = ''){
If ($ this-> is_log ){
$ Text = date ("Y-m-d H: I: s"). "". $ msg. "\ r \ n ";
Fwrite ($ this-> handle, $ text );
}
}

// Obtain the number of milliseconds
Public function microtime_float (){
List ($ usec, $ sec) = explode ("", microtime ());
Return (float) $ usec + (float) $ sec );
}
}

?>

Usage

The Code is as follows:

$ Db = new DB ();
Call
$ Db-> insert_id (); // obtain the latest IP Address

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.