Using PEAR to buffer PHP program 2_php tutorial

Source: Internet
Author: User
Use PEAR to buffer PHP program 2. Finally, we will define an application to comprehensively explain the overall framework of the PEAR buffer mechanism. We define a class called MySQL_Query_Cache to buffer the query results of SELECT. Finally, let's define an application to explain the overall framework of the PEAR buffer mechanism. We define a class called MySQL_Query_Cache to buffer the query results of SELECT. First, we define the class variables: '. ', 'Filename _ prefix' => 'cache _'), $ expires = 3600) {$ this-> cache ($ container, $ container_options ); $ this-> expires = $ expires;} function _ MySQL_Query_Cache () {if (is_resource ($ this-> connection) {mysql_close ($ this-> connection );} $ this-> _ Cache () ;}}?> Before the official start, we need some auxiliary functions. Function connect ($ hostname, $ username, $ password, $ database) {$ this-> connection = mysql_connect ($ hostname, $ username, $ password) or trigger_error ('database connection failed! ', E_USER_ERROR); mysql_select_db ($ database, $ this-> connection) or trigger_error ('database selection failed! ', E_USER_ERROR);} function fetch_row () {if ($ this-> cursor <sizeof ($ this-> result )) {return $ this-> result [$ this-> cursor ++];} else {return false;} function num_rows () {return sizeof ($ this-> result) ;}?> The following describes how to buffer data: Result = $ this-> get ($ cache_id, 'MySQL _ query_cache '); if ($ this-> result = NULL) {// buffer loss $ this-> cursor = 0; $ this-> result = array (); if (is_resource ($ this-> connection )) {// use mysql_unbuffered_query () if (function_exists ('MySQL _ unbuffered_query ') as much as possible) {$ result = mysql_unbuffered_query ($ query, $ this-> connection );} else {$ result = mysql_query ($ query, $ this-> connection) ;}// retrieve all query results while ($ row = Mysql_fetch_assoc ($ result) {$ this-> result [] = $ row;} // release the MySQL result resource mysql_free_result ($ result ); // buffer the result $ this-> save ($ cache_id, $ this-> result, $ this-> expires, 'MySQL _ query_cache ');}}} else {// no query results, no need to buffer return mysql_query ($ query, $ this-> connection) ;}}?> Example 3: use MySQL Query buffer Connect ('hostname', 'username', 'password', 'database'); $ cache-> query ('select * from table '); while ($ row = $ cache-> fetch_row () {echo'

'; Print_r ($ row); echo'
';}?> <全文完>

The overall framework of the elastic PEAR buffer mechanism. We define a class called MySQL_Query_Cache to buffer the query results of SELECT. I...

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.