Home  >  Article  >  Backend Development  >  Using PEAR to buffer PHP program 2_PHP tutorial

Using PEAR to buffer PHP program 2_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:29:04953browse

Finally, let's customize an application to comprehensively explain the overall framework of the PEAR buffering mechanism. We define a class called MySQL_Query_Cache to cache SELECT query results. We first 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 we 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 result)) { return $this->result[$this- >cursor++]; } else { return false; } } function num_rows() { return sizeof($this->result); } ?> Let’s see how to buffer: 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 possible if (function_exists('mysql_unbuffered_query')) {$result = mysql_unbuffered_query($query, $this->connection); } else {$result = mysql_query($query, $this-> connection); } // Fetch all query results while ($row = mysql_fetch_assoc($result)) {$this->result[] = $row; } // Release the MySQL result resource mysql_free_result($result); // Put the result Buffer $this->save($cache_id, $this->result, $this->expires, 'mysql_query_cache'); } } } else { // No query results, no buffering required return mysql_query($query, $this- >connection); } } ?> Example 3: Using MySQL query cache connect('hostname', 'username', 'password', 'database'); $cache->query('select * from table') ; while ($row = $cache->fetch_row()) { echo '


'; print_r($row); echo '
'; } ?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531729.htmlTechArticleFinally, let’s customize an application to comprehensively explain the overall framework of the PEAR buffering mechanism. We define a class called MySQL_Query_Cache to cache SELECT query results. I...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn