PHP static page implementation _ PHP Tutorial

Source: Internet
Author: User
Static PHP pages .? Objects) 60*3) {return the static file content to the client $ start_timemi
// First view the cached file if (file_exists ("static.html") {// The cache time is 3 minutes if (time ()-filemtime ("static.html") <60*3) {// return the static file content to the client $ start_time = microtime (); echo "the data I read from the static file :"."
"; Echo file_get_contents (" static.html "); $ end_time = microtime (); echo" static file usage time :". ($ end_time-$ start_time); exit ;}// if this is the first access or the last cache time exceeds 3 minutes, read data from the database $ host = "127.0.0.1"; $ user = "root"; $ password = "123456"; // record start time $ start_time = microtime (); mysql_connect ($ host, $ user, $ password); mysql_select_db ("mydb"); mysql_query ("set names utf8"); $ SQL = "SELECT name, address, email FROM users "; $ resource = mysql_query ($ SQL); echo" data read FROM the database:
"; Ob_start (); // enable the output buffer echo"

The users table contains three records using the apache service. The test results are as follows:
The average execution time of reading data from the database is about 0.0008041s.

Average execution time of direct read cache files: 0.0000475

There are only three records in the database, and SQL is also a simple single-table query. when there are many records in the table, or multi-table query, the execution time will be longer. Although the cache can reduce the number of visits to the database and accelerate the response time, the cache is not suitable for all pages. Some pages may change the content displayed on their pages each time they are accessed. such pages obviously cannot be cached. Cache is suitable for pages with few changes.

Http://www.bkjia.com/PHPjc/371932.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/371932.htmlTechArticle? Php // The first-in-first-out slow-saving file if(file_exists(static.html) {// cached) 60*3) {// return the static file content to the client $ start_time = mi...

"; // Output the obtained information while ($ userInfo = mysql_fetch_assoc ($ resource) {echo" "; Echo" "; Echo" "; Echo" "; Echo" ";}$ End_time = microtime (); $ str = ob_get_contents (); // Obtain the buffer content ob_end_flush (); echo" time when data is read from the database :". ($ end_time-$ start_time); file_put_contents ("static.html", $ str);?>
Name Address Email
". $ UserInfo ['name']."". $ UserInfo ['address']."". $ UserInfo ['email ']."

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.