Encapsulate your own DB Class (PHP) and db Class php. Encapsulate your own DB Class (PHP). encapsulate db Class php to encapsulate a DB class for special database operations. any operations on the database in the future will be implemented by DB class objects. In this way, you can encapsulate your own DB Class (PHP) and db Class php.
Encapsulate a DB class for database operations. all operations on the database in the future will be implemented by DB class objects. In this way, with your own DB class, you do not need to write a simple SQL statement every time when writing a project, just call it directly, which is very convenient!
1. encapsulate a DB class. A class file should have only one class, and no other content exists. Naming rules for class files: class name. class. php
The following code creates a DB class:
'Localhost', 'port' => '123'); */public function _ construct ($ arr = array ()) {// initialize $ this-> host = isset ($ arr ['host'])? $ Arr ['host']: 'localhost'; // First, determine whether your host exists. If yes, use your host, otherwise, use the default localhost $ this-> port = isset ($ arr ['port'])? $ Arr ['port']: '000000'; $ this-> user = isset ($ arr ['user'])? $ Arr ['user']: 'root'; $ this-> pass = isset ($ arr ['pass'])? $ Arr ['pass']: 'root'; $ this-> dbname = isset ($ arr ['dbname'])? $ Arr ['dbname']: 'mydatabase'; $ this-> charset = isset ($ arr ['charset'])? $ Arr ['charset']: 'utf8'; $ this-> prefix = isset ($ arr ['prefix'])? $ Arr ['prefix']: ''; // connect to the database (the class is to operate the database, so you need to connect to the database) $ this-> connect (); // set the character set $ this-> setCharset (); // select database $ this-> setDbname ();}/** connect to database */private function connect () {// mysql extended connection $ this-> link = mysql_connect ($ this-> host. ':'. $ this-> port, $ this-> user, $ this-> pass); // if (! $ This-> link) {// error in result // brute force processing. if a real online project (production environment) must be written to the log file echo 'database connection error:
'; Echo' error code '. mysql_errno ().'
'; Echo' error content '. mysql_error ().'
'; Exit ;}/ ** set character set */private function setCharset () {// set $ this-> db_query ("set names {$ this-> charset}");}/** select database */private function setDbname () {$ this-> db_query ("use {$ this-> dbname}");}/** add data * @ param1 string $ SQL, the insert statement * @ return boolean to be executed. if the return value is successful, the ID increases automatically. if the return value is failed, FALSE */public function db_insert ($ SQL) is returned) {// send data $ this-> db_query ($ SQL); // The Auto-increment ID return mysql_affected_rows () is returned successfully ()? Mysql_insert_id (): FALSE;}/** delete data * @ param1 string $ SQL, the delete statement to be executed * @ return Boolean, the number of affected rows is returned successfully, return FALSE */public function db_delete ($ SQL) {// send SQL $ this-> db_query ($ SQL); // return mysql_affected_rows ()? Mysql_affected_rows (): FALSE;}/** UPDATE data * @ param1 string $ SQL, the update statement to be executed * @ return Boolean, the number of affected rows is returned successfully, FALSE */public function db_update ($ SQL) {// send SQL $ this-> db_query ($ SQL); // return mysql_affected_rows ()? Mysql_affected_rows (): FALSE;}/** query: query a record * @ param1 string $ SQL. the SQL statement to be queried * @ return mixed. an array is returned successfully, return FALSE */public function db_getRow ($ SQL) {// send SQL $ res = $ this-> db_query ($ SQL ); // Determine Whether return mysql_num_rows ($ res) is returned )? Mysql_fetch_assoc ($ res): FALSE;}/** query: query multiple records * @ param1 string $ SQL, the SQL statement to be queried * @ return mixed, A two-dimensional array is returned successfully, and FALSE */public function db_getAll ($ SQL) {// send SQL $ res = $ this-> db_query ($ SQL ); // Determine if (mysql_num_rows ($ res) {// looping $ list = array (); // traversing while ($ row = mysql_fetch_assoc ($ res )) {$ list [] = $ row;} // return $ list;} // return FALSE;}/** mysql_query error handling * @ param1 string $ sq L. The SQL statement * @ return mixed to be executed. If no error occurs in the statement, all SQL statements */private function db_query ($ SQL) are returned) {// send SQL $ res = mysql_query ($ SQL); // determine the result if (! $ Res) {// error in result // brute-force processing. if a real online project (production environment) must be written to the log file echo, an error occurs:
'; Echo' error code '. mysql_errno ().'
'; Echo' error content '. mysql_error ().'
'; Exit;} // no error return $ res;} // _ sleep method public function _ sleep () {// return the array return array ('host', 'port', 'user', 'pass', 'dbname', 'charset ', 'prefix');} // _ wakeup method public function _ wakeup () {// connection resource $ this-> connect (); // set the character set and select the database $ this-> setCharset (); $ this-> setDbname ();}/** get the complete table name */protected function getTableName () {// Complete table name: prefix + table name return $ this-> prefix. $ this-> table ;}/// this DB Class. generally, no destructor is written (resources are not released)
2. use the DB Class. to create an object using a class, make sure that the class has been loaded into the code area. You can use oneMagic functionsTo automatically load classes.
Automatic loading: When a script file executes some statements (instantiation), it needs to find the corresponding class in the code area. if it cannot be found, it will load the corresponding class file by automatically loading the function.
Magic functions:__ Autoload ()
For example, if we need to use the DB class on the index. php page, we can call it directly. the specific code is as follows:
'Mydatabase '));
Future (PHP) encapsulates the db Class php and encapsulates a DB class for special operations on the database. all operations on the database in the future will be implemented by the DB Class object. So you have yourself...