The simple encapsulation code of some functions of the mysql module in php is as follows:
Class mysql {private $ db; // datebase connect private $ result; // mysql result static private $ mysql; // mysql object private function _ construct () {// The work before Create an object $ this-> db = mysql_connect ('localhost', 'root', ''); mysql_select_db ('hello ', $ this-> db);} public static function getObject () {// if have a object, return that object, Not create if (! Self: $ mysql instanceof self) self: $ mysql = new self; return self: $ mysql;} public function query ($ SQL) {$ this-> result = mysql_query ($ SQL, $ this-> db); return $ this-> result;} public function fetch () {if (isset ($ this-> result) return mysql_fetch_assoc ($ this-> result);} public function error () {return 'Error :'. mysql_error ();} public function num () // for SQL select result {return mysql_num_rows ($ this-> result);} public function close () {// return true or false return mysql_close ($ this-> db );}}