Selecting rows from the database using ADOdb

In the following example, I am going to query a MySQL table, loop through the results, and print them out to the screen, using PHP and ADOdb.

For the example, I have a table called “tablea”, which has two columns, “id” and “description”.

Also, $db is the database connection, for the avoidance of any doubt.

$result = $db->Execute("select id, description from tablea");
 if ($result === false) die("failed");
 while (!$result->EOF) {
     echo " <p>.$result->fields['id']." - ".$result->fields['description']." </p>";   

If you want to use ADOdb’s result caching system, the following change to line one will cache the results for 300 seconds.

$result = $db->CacheExecute(300,"select id, description from tablea");

Cache control in PHP

Set a cache control expiry for a PHP page. This will mean a repeat visitor will only request a new copy of the page if their cached version is older than the date/time specified in the Expires setting.

//sets an expiry of plus one hour
header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600)); 
header('Cache-Control: public');
header('Pragma: cache');

Ensure that you do this before sending any content to the browser.

You can use this cache cheatsheet to determine how many seconds you want to enter in the code above.

I would only recommend using cache control for php pages that are either very large, or that require a lot of computing power to generate. Also, only use this if you do not plan on updating the page within the expiry timeframe. There is no easy way for you to force a user to retrieve a fresh copy from your site. You could ask them to refresh their cache by hitting F5, but that’s not ideal.

You could also use your web server (Apache, or nginx) to determine the cache control, but I dont tend to use cache control for php files, and instead use it for static files, like css, javascript or images.