My DataBase class. My DataBase class solution ** DataBase configuration class * classDBConfig {publicstatic $ HOSTlocalhost; publicstatic $ USERNAMEroot; publicstatic $ PASSWORDroot; my DataBase class
Solution
/**
* Database configuration
*/
Class DBConfig
{
Public static $ HOST = 'localhost ';
Public static $ USERNAME = 'root ';
Public static $ PASSWORD = 'root ';
Public static $ DATABASE = 'shopping ';
Public static $ CHARSET = 'utf8 ';
}
?>
/**
* Database operations
*/
Class DataBase {
Private $ connection;
/**
* Constructor
* @ Access public
*/
Public function _ construct (){
$ CONFIG = require (dirname (_ FILE _). '/DBConfig. class. php ');
$ This> connection = mysql_connect (DBConfig: $ HOST, DBConfig: $ USERNAME, DBConfig: $ PASSWORD );
Mysql_select_db (DBConfig: $ DATABASE );
Mysql_query ("set names '". DBConfig: $ CHARSET ."'");
}
/**
* Structure method
* @ Access public
*/
Public function _ destruct (){
Mysql_close ($ this> connection );
}
/**
* Execute SQL query statements
* @ Access private
* @ Param string $ p_ SQL query command
* @ Return array record set, no record returns an empty array
*/
Private function query ($ p_ SQL ){
$ DataTemp = mysql_query ($ p_ SQL, $ this> connection );
$ Data = array ();
$ DataItem = 0;
While ($ rows = mysql_fetch_assoc ($ dataTemp )){
$ Data [$ dataItem] = $ rows;
$ DataItem ++;
}
Return $ data;
}
/**
* Execute SQL statements
* @ Access public
* @ Param string $ p_ SQL the SQL statement to be executed. it can be INSERT, SELECT, UPDATE, or DELETE.
* @ Return if SQL is SELECT, the record set is returned. if SQL is INSERT, the new record ID is returned. if SQL is UPDATE or DELETE, the affected number of rows is returned.
*/
Public function execute ($ p_ SQL ){
$ Controlr = strtoupper (substr ($ p_ SQL, 0, 6 ));
Switch ($ controlr ){
Case 'insert ':
Mysql_query ($ p_ SQL, $ this> connection );
$ Result = mysql_insert_id ($ this> connection );
Break;
Case 'select ':
$ Result = $ this> query ($ p_ SQL, $ this> connection );
Break;
Default:
Mysql_query ($ p_ SQL, $ this> connection );
$ Result = mysql_affected_rows ($ this> connection );
Break;
}
Return $ result;
}
}
?>
The call is simple:
$ SQL = '...'; // it can be any addition, deletion, modification, and query statement.
$ Db = new DataBase ();
$ Rs = $ db> execute ($ SQL );
$ Db = null;
Please kindly advise!
[]
D8888D reply content
Let's take a look ~~ [Img] quit
Restore DataBase solution/*** DataBase configuration class */class DBConfig {public static $ HOST = 'localhost'; public static $ USERNAME = 'root '; public static $ PASSWORD = 'root ';...