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...