Finally, let's customize an application that is integrated to explain the overall framework of the PEAR buffering mechanism. We define a class called Mysql_query_cache, which buffers the query results of SELECT. We first define the variables of the class: '. ', ' 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-& Gt;_cache (); }}?> We need some auxiliary functions before the official start. 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),?> below we look at how to buffer: result = $this->get ($cache _id, ' Mysql_query_cache '); if ($this->result = = NULL) {//buffer lost $this->cursor = 0; $this->result = Array (); if (Is_resource ($this->connec tion) {//Use Mysql_unbuffered_query () if Possible (function_exists (' Mysql_unbuffered_query ') {$result = Mysql_unbuffered_ Query ($query, $this->connection); } else {$result = mysql_query ($query, $this->connection);}//Remove all query results while ($row = Mysql_fetch_assoc ($result)) {$thi S->result[] = $row; }//Release MySQL result resource Mysql_free_result ($result); Buffer the results $this->save ($cache _id, $this->result, $this->expires, ' Mysql_query_cache '); }}}} else {//No query results required, no buffer return mysql_query ($query, $this->connection);}} ?> Example 3: Using MySQL query buffering Connect (' hostname ', ' username ', ' password ', ' database '); $cache->query (' SELECT * from table '); while ($row = $cache->fetch_row ()) {echo '
’; Print_r ($row); Echo '
’; }?> <全文完> <全文完>
http://www.bkjia.com/PHPjc/531729.html www.bkjia.com true http://www.bkjia.com/PHPjc/531729.html techarticle Finally, let's customize an application that is integrated to explain the overall framework of the PEAR buffering mechanism. We define a class called Mysql_query_cache, which buffers the query results of SELECT. I ...