My DataBase class_php tutorial

Source: Internet
Author: User
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 ';...

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.