Phpcall () method for database coherent operations

Source: Internet
Author: User
The phpcall () method implements database coherent operations. Background: for jquery, there is a kind of dom called chained operations. PHP can also use similar methods when writing SQL statements, such:

/** Obtain the user's album records from the database ** @ param str | array $ where query condition * @ param int $ page current page * @ param int $ perpage records displayed on each page count * @ param str $ order sorting type * @ param str $ asc ascending/descending * @ return array records that meet the conditions/public function getUsersAlbumFromDB ($ where, $ page, $ perpage, $ order, $ asc) {$ offset = ($ page-1) * $ perpage; $ dataList = $ this-> getSelectObj () -> where ($ where)-> order ($ order, $ asc)-> limit ($ perpage, $ offset)-> fetchObject (); return $ dataList ;}

This method is similar to Jquery's chained method, as follows:

$ (this).find ("p").css ("background", "red").end().siblings ().find ("p").css ("background", "green");

_ Call function introduction: http://justwinit.cn/post/2834/

========================================================== ==========

Use the _ call () method to achieve consistent database operations

 "", "Where" => "", "order" => "", "limit" => "", "group" => "", "having" => "",); // call the field () where () order () limit () group () having () method, combined SQL statement function _ call ($ methodName, $ args) {// convert the first parameter (representing the method name without a method) to lowercase, GET method name $ methodName = strtolower ($ methodName); // If the called method name corresponds to the member attribute array $ SQL subscript, the second parameter is given to the element if (array_key_exists ($ methodName, $ this-> SQL) corresponding to the subscript in the array )) {$ this-> SQL [$ methodName] = $ args [0];} else {Echo 'Call class '. get_class ($ this ). 'method in '. $ methodName. '() does not exist';} // returns your own object, you can continue to call the methods in this object to form a consistent operation return $ this ;} // an SQL statement that is combined after a coherent operation is output. it is the final method of a coherent operation, function select () {echo "SELECT {$ this-> SQL ['field']} FROM user {$ this-> SQL ['where']} {$ this-> SQL ['order'] }{$ this-> SQL ['limit'] }{$ this-> SQL ['group'] }{$ this-> SQL ['having']} "; }} $ db = new Db (); // coherent operations $ db-> field ('sex, count (sex) ')-> where ('Where sex in ("male "," Female ") ')-> group ('group by sex')-> having ('Having avg (age)> 25')-> select ();?>

---------- Debug PHP ----------

SELECT sex, count (sex) FROM user where sex in ("male", "female") group by sex

Having avg (age)> 25

Output completed (0 sec consumed)-Normal Termination

From: http://blog.sina.com.cn/s/blog_6109978501017154.html

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.