Recently, pdo was used in the project. I checked the manual and found that the functions were similar. so I encapsulated a pdo class using interfaces, achieve the same implementation as mysql.
Implementation of mysql and pdo operations in the php mysql database
Recently, pdo was used in the project. I checked the manual and found that the functions were similar. so I encapsulated a pdo class using interfaces, achieve the same implementation as mysql.
2. [File] MySqlDB. class. php
_ InitServer ($ config); // initialize the server information $ this-> _ connectServer (); // link the server $ this-> _ setCharset (); // set the character set encoding $ this-> _ selectDB (); // select the default database} private function _ clone () {echo "this object cannot be cloned ","
"; Die ();} private static $ _ instance; public static function getInstance ($ config = array () {if (! (Static ::$ _ instance instanceof static) {static ::: _ instance = new static ($ config);} return static ::$ _ instance ;} private function _ initServer ($ config) {$ this-> _ host = isset ($ config ['host'])? $ Config ['host']: 'localhost'; $ this-> _ port = isset ($ config ['port'])? $ Config ['port']: '000000'; $ this-> _ user = isset ($ config ['user'])? $ Config ['user']: ''; $ this-> _ password = $ config ['password']; $ this-> _ charset = isset ($ config ['charset'])? $ Config ['charset']: 'utf8'; $ this-> _ dbname = isset ($ config ['dbname'])? $ Config ['dbname']: 'test';} private function _ connectServer () {$ connect_result = @ mysql_connect ("$ this-> _ host: $ this-> _ port ", $ this-> _ user, $ this-> _ password); if ($ connect_result) {$ this-> _ link = $ connect_result ;} else {echo 'database connection failed. please confirm the server information'; die () ;}} private function _ setCharset () {$ SQL = "set names $ this-> _ charset"; $ this-> query ($ SQL);} private function _ selectDB () {$ SQL = "USE '$ thi S-> _ dbname "'; $ this-> query ($ SQL);}/*** execute the SQL statement * @ param string $ SQL * @ return mixed execution result. Query-type SQL statements (select, show, desc). a result set resource is returned successfully. if a result set resource fails, false is returned. For non-query classes (insert, delete, update), true is returned if the query succeeds, and false is returned if the query fails. */public function query ($ SQL) {$ query_result = @ mysql_query ($ SQL, $ this-> _ link); if (false = $ query_result) {echo "SQL execution failed :","
"; Echo" incorrect SQL :","
", $ SQL ,"
"; Echo" error message :","
", Mysql_errno ($ this-> _ link ),"
"; Die ();} else {return $ query_result;}/*** @ param string $ SQL is usually: select * from... * @ return array */public function fetchRow ($ SQL) {$ result = $ this-> query ($ SQL); $ row = @ mysql_fetch_assoc ($ result ); @ mysql_free_result ($ result); return $ row;}/*** @ param string $ SQL is usually: select count (*) from... * @ return string NULL if no value is returned */public function fetchOne ($ SQL) {$ result = $ this-> query ($ SQL ); $ row = @ mysql_fetch_row ($ result); @ mysql_free_result ($ result); if ($ row) return $ row [0]; else return NULL ;} /*** @ param string $ SQL is usually: select * from... where .. like 'Han % '* @ return array */public function fetchAll ($ SQL) {$ result = $ this-> query ($ SQL); $ rows = array (); while ($ row = @ mysql_fetch_assoc ($ result) $ rows [] = $ row; @ mysql_free_result ($ result); return $ rows;}/** close the connection to the current database, generally, you do not need to use it. the connection will be automatically closed as the php script ends * // * public function close () {return @ mysql_close ($ this-> _ link );} * // *** prevent SQL injection: escape strings, use the * @ param string $ str character string with escape characters in the model * @ return string the character string after escaping with quotation marks */public function escapeString ($ str = '') {return "'". mysql_real_escape_string ($ str, $ this-> _ link ). "'";}}
3. [File] PDODB. class. php
_ InitServer ($ config); $ this-> _ newPDO ();} private function _ initServer ($ config) {$ this-> _ host = isset ($ config ['host'])? $ Config ['host']: 'localhost'; $ this-> _ port = isset ($ config ['port'])? $ Config ['port']: '000000'; $ this-> _ user = isset ($ config ['user'])? $ Config ['user']: ''; $ this-> _ password = $ config ['password']; $ this-> _ charset = isset ($ config ['charset'])? $ Config ['charset']: 'utf8'; $ this-> _ dbname = isset ($ config ['dbname'])? $ Config ['dbname']: 'test';} private function _ newPDO () {// set the parameter $ this-> _ setDSN (); // Set the data source parameter $ this-> _ setOption (); // set the option $ this-> _ getPDO (); // Obtain the PDO object} private function _ setDSN () {$ this-> _ dsn = "mysql: host = $ this-> _ host; port = $ this-> _ port; dbname = $ this-> _ dbname ";} private function _ setOption () {$ this-> _ option = array (PDO: MYSQL_ATTR_INIT_COMMAND => "set names $ this-> _ charset ");} private function _ getPDO () {$ this-> _ pdo = new PDO ($ this-> _ dsn, $ this-> _ user, $ this-> _ password, $ this-> _ option);} private function _ clone () {echo "this object cannot be cloned ","
"; Die ();} private static $ _ instance; public static function getInstance ($ config = array () {if (! (Static ::$ _ instance instanceof static) {static ::: _ instance = new static ($ config);} return static ::$ _ instance ;} // execution method. applicable scenarios: private static $ _ queryStr = array ("select", "show", "desc"); public function query ($ SQL = '') {// use regular expression Filtering. query and exec foreach (static: $ _ queryStr as $ str) {if (preg_match ("/^ \ s *". $ str. ". *? /I ", $ SQL) {// query class returned result set Object $ result = $ this-> _ pdo-> query ($ SQL );} else {// return bool $ result = $ this-> _ pdo-> exec ($ SQL) for non-query classes )! = False; // it may be 0} // if the execution fails, if ($ result = false) {$ error_info = $ this-> errorInfo (); echo "SQL execution failed :","
"; Echo" incorrect SQL :","
", $ SQL ,"
"; Echo" error message :","
", $ Error_info [2],"
"; Die () ;}else {return $ result;} break ;}} public function fetchAll ($ SQL = '') {$ result = $ this-> query ($ SQL); $ rows = $ result-> fetchAll (PDO: FETCH_ASSOC); $ result-> closeCursor (); return $ rows;} public function fetchRow ($ SQL = '') {$ result = $ this-> query ($ SQL); $ row = $ result-> fetch (PDO:: FETCH_ASSOC); $ result-> closeCursor (); return $ row;} public function fetchOne ($ SQL = '') {$ result = $ this-> query ($ SQL); $ string = $ result-> fetchColumn (); $ result-> closeCursor (); return $ string ;} public function escapeString ($ str = '') {return $ this-> _ pdo-> quote ($ str );}}
4. call in [code] model
_ InitDAO (); // initialize the basic model} protected function _ initDAO () {$ config = array ('host' => '***', 'user' => '***', 'password' => '', 'dbname' => '***'); // $ this-> _ dao = MySqlDB: getInstance ($ config); // call mysqldb $ this-> _ dao = PDODB: getInstance ($ config ); // call pdo }}
The above is the implementation of mysql and pdo operations in the php mysql database. For more information, see PHP Chinese network (www.php1.cn )!