: This article mainly introduces my PDO class. if you are interested in the PHP Tutorial, refer to it.
Dsn = isset ($ arr ['dsn '])? $ Arr ['dsn ']: 'MySQL: host = localhost; dbname = project;'; $ this-> user = isset ($ arr ['user'])? $ Arr ['user']: 'root'; $ this-> password = isset ($ arr ['password'])? $ Arr ['password']: '000000'; $ this-> pdo = new PDO ($ this-> dsn, $ this-> user, $ this-> password ); # instantiate a pdo object} // perform database operations using PDO/* @ param string $ SQL to determine SQL statements */private function _ Exec ($ SQL) {try {# handle errors $ res = $ this-> pdo-> exec ($ SQL); if (! $ Res) {# Statement error. throw new PDOException ('error! ');} Else {return $ res;} catch (PDOException $ e) {echo $ e-> getMessage (); # output exception information echo"
"; Echo" error row: ". $ e-> getLine ()."
"; Echo" error No.: ". $ this-> pdo-> errorInfo () [1]."
"; Echo" error message: ". $ this-> pdo-> errorInfo () [2]."
"; Exit () ;}/// insert/** @ param string $ SQL * @ return int affected rows */public function db_insert ($ SQL) {$ res = $ this-> _ Exec ($ SQL); # execute the return $ this-> pdo-> lastInsertId ();} // Insert/** @ param string $ SQL * @ return int number of affected rows */public function db_delete ($ SQL) {$ affected = $ this-> _ Exec ($ SQL); # execute the return $ affected ;} // update/* @ param string $ SQL statement to insert @ retutn int number of affected rows */public function db_update ($ SQL) {$ affected = $ t His-> _ Exec ($ SQL); # execute the return $ affected; # return the number of affected rows} // check whether the query statement syntax is correct. private function Iserror ($ SQL) {try {// execute the statement $ res = $ this-> pdo-> query ($ SQL); if (! $ Res) {# Statement error. throw new PDOException ('error! ');} Return $ res; // The statement is correct} catch (PDOException $ e) {echo $ e-> getMessage (); # output error message echo"
"; Echo" error row: ". $ e-> getLine ()."
"; Echo" error No.: ". $ this-> pdo-> errorInfo () [1]."
"; Echo" error message: ". $ this-> pdo-> errorInfo () [2]."
"; Exit () ;}/// get/* @ param string $ SQL @ return array returns an array */public function db_getOne ($ SQL) {$ stmt = $ this-> Iserror ($ SQL); # execute an SQL statement for query. Unlike the exec method, $ row = $ stmt-> fetch (PDO: FETCH_ASSOC ); # return an Associated array return $ row ;} # obtain multi-row data/* @ param string $ the statement to be executed by SQL @ return array returns an array */public function db_getAll ($ SQL) {$ stmt = $ this-> Iserror ($ SQL); $ rows = $ stmt-> fetchAll ($ stmt, PDO: FETCH_ASSOC); return $ rows ;} // pdo transaction processing/* Tr An_start start the transaction @ param string $ sp save point. The default value is sp1 @ return boolean */public function db_tran_start ($ sp = 'sp1 ') {# run the statement, start transaction $ res = $ this-> pdo-> beginTransaction (); return true;}/* transaction commit @ return boolean */public function db_tran_commit () {$ this-> pdo-> commit ();} // transaction rollBack public function db_tran_rollback () {$ this-> rollBack ();} // pre-compile/* @ param string $ SQL # What is used in SQL statements? As a placeholder @ param array $ params @ param array $ arr */public function db_prepare ($ SQL, $ params) {# prepare pre-compiled statements do not require Iserror to Judge $ stmt = $ this-> pdo-> prepare ($ SQL); # execute statements $ stmt-> execute ($ params ); $ res = $ stmt-> fetchAll (PDO: FETCH_ASSOC); return $ res ;}}
The above introduces my PDO class, including some content. I hope my friends who are interested in the PHP Tutorial can help me.