Introduction to The ADOdb class library in PHP (2)

Source: Internet
Author: User
Last month, we briefly learned how to perform SELECT, INSERT, and UPDATE operations in ADOdb. If you are a newbie in ADOdb, I suggest you read the article last month first. ADOdb has many more advanced features. This month, we will pay attention to some of them. Databases often become cache-based for low application efficiency

Last month, we briefly learned how to perform SELECT, INSERT, and UPDATE operations in ADOdb. If you are a newbie in ADOdb, I suggest you read the article last month first. ADOdb has many more advanced features. This month, we will pay attention to some of them.

Databases often become the secrets of inefficient applications. Minimizing database queries is one way to improve execution efficiency. This can usually be done by caching the entire page (there are many ways to achieve this. For example, PEAR-> Cache), or, if you need to make a dynamic page and want to Cache query commands, you can use ADOdb, simply cache query commands. Before your view improves the poor performance of your application through caching, I suggest you try to optimize your query commands first. Sometimes some simple indexes can change everything-too many so-called professional solutions are using bad indexes. In this article, you can find many such instances. Now, let's take a look at how ADOdb enables you to cache the database query results. In this instance, ADOdb stores the results of our last query in the/var/tmp/adodb_cache Cache file, and keeps it for 10 minutes.





Include ("$ adodb_path/db_values.inc.php ");
Include ("$ adodb_path/adodb. inc. php ");
$ Db = NewADOConnection ('$ database_type ');
$ Db-> Connect ("$ host", "$ user", "$ password", "employees ");

$ ADODB_CACHE_DIR = "/var/tmp/adodb_cache"; // Directory to store cached files

$ SQL = "SELECT surname, age FROM employees ";
$ Rs = & $ db-> CacheExecute (600, $ SQL); // Executes, and caches the results for 600 seconds
If (! $ Rs ){
Print $ db-> ErrorMsg (); // Displays the error message if no results cocould be returned
}
Else {
While (! $ Rs-> EOF ){
Print $ rs-> fields [0]. ''. $ rs-> fields [1].'
';
// Fields [0] is surname, fields [1] is age
$ Rs-> MoveNext (); // Moves to the next row
} // End while
} // End else

The CacheExecute () function has two parameters: the first parameter is the cache file retention time, in seconds; the second parameter is the SQL statement. The first parameter is optional (some developers may think it should be the second parameter). If you do not have a limited time, the default value is 3600 seconds, that is, 1 hour. The cached file is named adodb _ *. cache. you can safely delete it in the file system. You should regularly understand expired cache files (using UNIX "crontab perpetual calendar" or WINDOWS "Scheduled Tasks ". Note: perpetual calendar does not translate correctly. I do not have UNIX .). Note that to use the cache method, set magic_quotes_runtime of PHP to off (in php. ini, set the value to 0 ). You can modify its value at runtime as needed:
Set_magic_quotes_runtime (0 );
You only need to put the above code before you call the database command. You can clear the cache at any time by calling CacheFlush. For security considerations, ADOdb also recommends setting the PHP parameter register_globals to 0 (which is the default value in the latest PHP version ).


Related Article

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.