Using Pear to buffer PHP program two _php tutorial

Source: Internet
Author: User
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 ...

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