PHP database access

Source: Internet
Author: User
& Lt ;? Php *** dao=ract * @ authoreinar_he@76rens.com * abstractclassGospel_Dao_AbstractimplementsGospel_Dao_Interface {*** table name *** @ varstring * pr
 Init ();}/*** initialization operation */abstract protected function init (); /*** get the db source ** @ param string $ rule * @ return Gospel_Db_Adapter */public function getAdapter ($ rule = 'master') {return Gospel_Db_Adapter :: getInstance ($ rule);}/*** get db name ** @ param string $ rule * @ return string */public function getDatabase ($ rule = '') {return $ this-> _ database;}/*** name of the active table ** @ param string $ rule * @ return string */public function GetTable ($ rule = '') {return $ this-> _ table;}/* get multiple records * @ see Gospel/Dao/Gospel_Dao_Interface # getItems ($ rule, $ where, $ sort, $ offset, $ limit, $ pager, $ flag) */public function getItems ($ rule, $ where, $ sort = '', $ offset, $ limit, $ pager = 0, $ flag = 'Slave ') {$ return = array (); if ($ where) {$ query = 'select * from '; $ query. = $ this-> getTable ($ rule); $ query. = 'where '. $ this-> getWhere ($ where); if ($ sort) {$ query. = 'Order by'. $ sort;} $ page = $ page> 0? $ Page: 1; $ psize = $ psize> 0? $ Psize: 10; $ offset = ($ page-1) * $ psize; $ query. = 'limit '. $ offset. ','. $ psize; $ data = $ this-> getAdapter ($ flag)-> queryAll ($ query); if ($ pagination) {$ return ['data'] = array (); if ($ data) {$ rtotal = $ this-> getWhereTotal ($ rule, $ where ); $ return ['data'] = $ data; $ return ['pager'] = array ('page' => $ page, 'psize' => $ psize, 'rtotal' => $ rtotal, 'ptotal '=> ceil ($ rtotal/$ psize ));} else {$ return ['pager'] = array ('page' => $ page, 'p Size '=> $ psize, 'rtotal' => 0, 'ptotal' => 0) ;}} else {$ return = $ data ;}} return $ return ;} /* obtain a single record * @ see Gospel/Dao/Gospel_Dao_Interface # getItem ($ rule, $ where, $ sort, $ flag) */public function getItem ($ rule, $ where, $ sort = '', $ flag = 'Slave ') {$ return = array (); if ($ where) {$ query = 'select * from'; $ query. = $ this-> getTable ($ rule); $ query. = 'where '. $ this-> getWhere ($ where); if ($ sort! = '') {$ Query. = 'Order '. $ sort;} $ query. = 'limit '; $ return = $ this-> getAdapter ($ flag)-> queryRow ($ query);} return $ return ;} /* obtain table field information * @ see Gospel/Dao/Gospel_Dao_Interface # getTableFields ($ rule, $ flag) */public function getTableFields ($ rule, $ flag = 'master ') {return $ this-> getAdapter ($ flag)-> getTableFields ($ this-> getTable ($ rule ));} /* update record * @ see Gospel/Dao/Gospel_Dao_Interface # update ($ rule, $ wh Ere, $ set) */public function update ($ rule, $ where, array $ set) {$ return = 0; if ($ where & $ set) {$ query = 'update ''. $ this-> getTable ($ rule); $ query. = ''set '. $ this-> getSet ($ set); $ query. = 'where '. $ this-> getWhere ($ where); if ($ query) {$ return = $ this-> getAdapter ('Master')-> queryUpdate ($ query );}} return $ return;}/* Count of records * @ see Gospel/Dao/Gospel_Dao_Interface # getWhereTotal ($ rule, $ where, $ flag) */public Function getWhereTotal ($ rule, $ where, $ flag = 'Slave ') {$ return = 0; if ($ where) {$ SQL = 'SELECT COUNT (*) from '. $ this-> getTable ($ rule); $ SQL. = 'where '. $ this-> getWhere ($ where); $ return = $ this-> getAdapter ()-> queryTotal ($ SQL);} return $ return ;} /* add record * @ see Gospel/Dao/Gospel_Dao_Interface # add ($ rule, $ row) */public function add ($ rule, array $ row) {$ return = 0; if ($ row) {$ keys = ''; $ values =''; foreach ($ row As $ key => $ value) {$ keys. = '''. $ key. '', '; if (substr ($ key, 0, 1) = 'n') {$ values. = $ value. ',';} else {$ values. = "'". $ value. "'," ;}}$ keys = substr ($ keys, 0,-1); $ values = substr ($ values, 0,-1 ); $ query = 'Insert INTO ''; $ query. = $ this-> getTable ($ rule); $ query. = ''('. $ keys. ') VALUES ('; $ query. = $ values. ')'; $ return = $ this-> getAdapter ('Master')-> queryUpdate ($ query);} return $ return;}/* delete record * @ see Gospel /Dao/Gospel_Dao_Interface # del ($ rule, $ where, $ lines) */public function del ($ rule, $ where, $ lines = 1) {$ return = 0; if ($ where) {$ query = 'delete FROM '''; $ query. = $ this-> getTable ($ rule); $ query. = ''where '. $ this-> getWhere ($ where); $ query. = 'limit '. $ lines; $ return = $ this-> getAdapter ('Master')-> queryUpdate ($ query);} return $ return ;} /*** format the where statement ** @ param string | array $ where * @ return string */priv Ate function getWhere ($ where) {$ return = ''; if (is_array ($ where) {$ return. = implode ('and', $ where);} else {$ return. = $ where;} return $ return;}/*** format to update statement format ** @ param array $ set * @ return String */private function getSet (array $ set) {$ return = ''; if ($ set) {foreach ($ set as $ key => $ val) {if (substr ($ key, 0, 1 )! = 'N') {$ return. = '''. $ key. "'= '". $ val. "',";} else {$ mk = substr ($ val, 0, 1); if (is_numeric ($ mk) {$ return. = '''. $ key. "'= ". $ val. "," ;}else {$ v = intval (substr ($ val, 1); if ($ mk = '+') {$ return. = '''. $ key. ''= ''. $ key. ''+ '. $ v. ',';} elseif ($ mk = '-') {$ return. = '''. $ key. ''= ''. $ key. ''-'. $ v. ',' ;}}$ return = substr ($ return, 0,-1);} return $ return ;}// end

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.