Phpmysqlpdo operations-PHP source code

Source: Internet
Author: User
Phpmysqlpdo operation class provides mysql addition, deletion, modification, query, and other operations

[Code] [PHP] code

Class pdomysql {public static $ dbtype = 'mysql'; public static $ dbhost = ''; public static $ dbport =''; public static $ dbname = ''; public static $ dbuser = ''; public static $ dbpass =''; public static $ charset = ''; public static $ stmt = null; public static $ DB = null; public static $ connect = true; // whether the connection is persistent public static $ debug = false; private static $ parms = array ();/*** constructor */public funct Ion _ construct () {self: $ dbtype = 'mysql'; self: $ dbhost = HOST; self: $ dbport = '000000'; self :: $ dbname = 'exception'; self: $ dbuser = 'manager'; self: $ dbpass = '000000'; self: $ connect = true; self :: $ charset = 'utf8'; self: connect (); self ::$ DB-> setAttribute (PDO: MYSQL_ATTR_USE_BUFFERED_QUERY, true); self :: $ DB-> setAttribute (PDO: ATTR_EMULATE_PREPARES, true); self: execute ('set names '. self :: $ Charset);}/*** destructor */public function _ destruct () {self: close ();} ************** * ****** // *** function: connected database */public function connect () {try {self: $ DB = new PDO (self: $ dbtype. ': host = '. self: $ dbhost. '; port = '. self: $ dbport. '; dbname = '. self: $ dbname, self: $ dbuser, self: $ dbpass, array (PDO: ATTR_PERSISTENT => self: $ connect);} catch (PDOE Xception $ e) {die ("Connect Error Infomation :". $ e-> getMessage () ;}/ *** close data connection */public function close () {self ::$ DB = null ;} /*** define strings */public function quote ($ str) {return self: $ DB-> quote ($ str );} /*** function: Get the struct bit in the data table * return: table field structure * type: Array */public function getFields ($ table) {self ::$ stmt = self:: $ DB-> query ("DESCRIBE $ table"); $ result = self ::$ stmt-> fetchAll (PDO: FETCH_AS SOC); self ::$ stmt = null; return $ result;}/*** function: obtain the primary region ID of the last INSERT * and return: last INSERT primary ID * type: number */public function getLastId () {return self: $ DB-> lastInsertId () ;}/ *** function: INSERT \ UPDATE \ DELETE * return: number of rows affected by the execution of statement * type: number */public function execute ($ SQL) {self: getPDOError ($ SQL ); return self: $ DB-> exec ($ SQL);}/*** get the data to be operated * return: SQL statement after merging * type: string */private function getCode ($ table, $ args) {$ Code = ''; if (is_array ($ args) {foreach ($ args as $ k => $ v) {if ($ v = '') {continue;} $ code. = "'$ k' =' $ V'," ;}}$ code = substr ($ code, 0,-1); return $ code ;} public function optimizeTable ($ table) {$ SQL = "optimize table $ table"; self: execute ($ SQL);}/*** execute a specific SQL operation * return: result * type: array */private function _ fetch ($ SQL, $ type) {$ result = array (); self ::$ stmt = self :: $ DB-> quer Y ($ SQL); self: getPDOError ($ SQL); self: $ stmt-> setFetchMode (PDO: FETCH_ASSOC); switch ($ type) {case '0': $ result = self ::$ stmt-> fetch (); break; case '1': $ result = self :: $ stmt-> fetchAll (); break; case '2': $ result = self: $ stmt-> rowCount (); break;} self: $ stmt = null; return $ result ;} /********************* end of the basic method ************* ******** // **************************** start the SQL operation method **** *** * ************ // **** Function: insert data * return: table inrecord * type: Array * records: $ db-> insert ('$ table', array ('title' => 'zxsv') */public function add ($ table, $ args) {$ SQL = "insert into '$ table 'set"; $ code = self: getCode ($ table, $ args); $ SQL. = $ code; return self: execute ($ SQL);}/*** modify data * return: number of records * type: number * records: $ db-> update ($ table, array ('title' => 'zxsv'), array ('id' => '1 '), $ where * = 'Id = 3'); */public function update ($ Table, $ args, $ where) {$ code = self: getCode ($ table, $ args); $ SQL = "updat' $ table 'set "; $ SQL. = $ code; $ SQL. = "Where $ where"; return self: execute ($ SQL);}/*** function: division of data * return: table recording * type: array * Records: $ db-> delete ($ table, $ condition = null, $ where = 'Id = 3') */public function delete ($ table, $ where) {$ SQL = "delete from '$ table' Where $ where"; return self: execute ($ SQL);}/*** function: get single row data * Return: The first record in the table * type: Array * Records: $ db-> fetOne ($ table, $ condition = null, $ field = '*', $ where = '') */public function fetOne ($ table, $ field = '*', $ where = false) {$ SQL = "SELECT {$ field} FROM '{$ table}"'; $ SQL. = ($ where )? "WHERE $ where": ''; return self: _ fetch ($ SQL, $ type = '0');}/*** function: Get all data * return: table recording * Type: Two-digit array * records: $ db-> fetAll ('$ table', $ condition = '', $ field = '*', $ orderby = '', $ limit * ='', $ where = '') */public function fetAll ($ table, $ field = '*', $ orderby = false, $ where = false) {$ SQL = "SELECT {$ field} FROM '{$ table}"'; $ SQL. = ($ where )? "WHERE $ where": ''; $ SQL. = ($ orderby )? "Order by $ orderby": ''; return self: _ fetch ($ SQL, $ type = '1') ;}/ *** function: get data in a single row * Return: The first record in the table * type: Array * Records number: select * from table where id = '1' */public function getOne ($ SQL) {return self: _ fetch ($ SQL, $ type = '0');}/*** function: Get all data * return: table inrecord * type: two distinct array * distinct number: select * from table */public function getAll ($ SQL) {return self: _ fetch ($ SQL, $ type = '1 ');} /*** function: get the first column of data in the first line * return: the first column of the first line of the second value * class Type: value * Records: select 'A' from table where id = '1' */public function scalar ($ SQL, $ fieldname) {$ row = self :: _ fetch ($ SQL, $ type = '0'); return $ row [$ fieldname];}/*** retrieve the total number of records * return: number of records * type: number * partitions: $ db-> fetRow ('$ table', $ condition = '', $ where =''); */public function fetRowCount ($ table, $ field = '*', $ where = false) {$ SQL = "select count ({$ field}) AS num FROM $ table"; $ SQL. = ($ where )? "WHERE $ where": ''; return self: _ fetch ($ SQL, $ type = '0');}/*** get the total number of records * return: number of records * type: number * Records: select count (*) from table */public function getRowCount ($ SQL) {return self: _ fetch ($ SQL, $ type = '2 ');} /********************** end of the SQL operation method ************ ********* // **************************** error handling start **** * **************** // Set whether to set the debugging mode */public function setDebugMode ($ mode = true) {return ($ m Ode = true )? Self: $ debug = true: self: $ debug = false;}/*** capture PDO error information * return: Error message * type: string */private function getPDOError ($ SQL) {self: $ debug? Self: errorfile ($ SQL): ''; if (self: $ DB-> errorCode ()! = '20140901') {$ info = (self: $ stmt )? Self: $ stmt-> errorInfo (): self: $ DB-> errorInfo (); echo (self: sqlError ('MySQL Query error ', $ info [2], $ SQL); exit () ;}} private function getshortterror ($ SQL) {self ::$ debug? Self: errorfile ($ SQL): ''; if (self: $ stmt-> errorCode ()! = '20140901') {$ info = (self: $ stmt )? Self: $ stmt-> errorInfo (): self: $ DB-> errorInfo (); echo (self: sqlError ('MySQL Query error ', $ info [2], $ SQL); exit () ;}/ *** collect error logs */private function errorfile ($ SQL) {echo $ SQL.'
'; $ Errorfile = _ ROOT. '. /dberrorlog. php '; $ SQL = str_replace (array ("\ n", "\ r", "\ t", ""), array ("", "", ""), $ SQL); if (! File_exists ($ errorfile) {$ fp = file_put_contents ($ errorfile ," \ N ". $ SQL);} else {$ fp = file_put_contents ($ errorfile, "\ n ". $ SQL, FILE_APPEND) ;}}/*** function: run error message * return: run error message and SQL statement * type: character */private function sqlError ($ message = '', $ info ='', $ SQL = '') {$ html =''; if ($ message) {$ html. = $ message;} if ($ info) {$ html. = 'sqlid :'. $ info;} if ($ SQL) {$ html. = 'errorsql :'. $ SQL;} throw new Exception ($ html );} /********************** end of error handling ************* ********/}

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.