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>";   
     
	$result->MoveNext();
	
 }

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");

Stop WordPress admin menu hiding navigation bar

If your site has a horizontal navigation bar at the top of the screen, the WordPress admin menu can hide it when you are logged into your site.

To resolve this, you can use a php function – is_admin_bar_showing – to amend the layout of your site so that the menu is no longer obscured.

The following code will add in an empty div above the header menu, therefore pushing it down below the WordPress admin bar.

<?php 

  if ( is_admin_bar_showing() ) 
    echo '<div style="min-height: 28px;"></div>'; 
?>

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.