cache the database result in txt file and how to output it?
eg: $results = mysql_query('SELECT title FROM articl开发者_高级运维e');
now I want to put the result in a text file. Then :
$filepath = 'test.txt';
if (is_file($filepath)) {
echo file_get_contents($filepath);
} else {
// read the databse, and stored the result to the test.txt.
}
- How to wirite the else part?
- I want to echo the content in list style. how should I do?
Thank you
I really wouldn't.
Your filesystem 'cache' is likely to be slower than the database, and introduces extra bonus consistency problems on top of the added complexity.
If you have expensive, complex queries, look at putting the cached results in another database table, or memcache, or optimising/denormalising the database to make the query faster.
I would suggest you look at an opcode cache such as APC. This will handle the cache expiry for you, and will be significantly faster than either this filesystem cache or a SQL query.
I often use this technique handle slow queries, from read often, write little databases.
精彩评论