Using your suggestion of closing the $dbc connection I substitued the following code
mysql_free_result ($result); // Free up the resources.
mysql_close(); // Close the database connection.
for this:
mysql_close($dbc);
unset($dbc);
and it now works a treat, for some reason it really didn't like the mysql_close statement or the mysql_free_result. I fumbled through these possiblilities without really knowing what I'm doing so if anyone could explain why the code works please let me know - is the 'unset' statement more stable or reliable?
If anyone would like to use or adapt the code here is the complete page:
# - viewRetailers.php
// This script retrieves all the records from the retailers table.
// Include the config file for error management and such.
include ('includes/config.inc.php');
$page_title = 'View the Current Retailers';
include ('includes/header.html');
// If no first_name variable exists, redirect the user.
if (!isset($_SESSION['first_name'])) {
// Start defining the URL.
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
// Check for a trailing slash.
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1); // Chop off the slash.
}
// Add the page.
$url .= '/index.php';
ob_end_clean(); // Delete the buffer.
header("Location: $url");
exit(); // Quit the script.
} else { // Welcome the user by name if they are logged in.
echo '<strong>Welcome</strong>';
if (isset($_SESSION['first_name'])) {
echo " {$_SESSION['first_name']}";
}
}
// Page header.
echo '<p><h2>What on Earth Retailers</h2></p>';
require_once ('../../mysql_connect.php'); // Connect to the db.
$result = mysql_query("SELECT * FROM retailers");
$num_rows = mysql_num_rows($result);
echo "There are currently <strong>$num_rows</strong> retailers that sell our products<br />";
// Number of records to show per page:
$display = 3;
// Determine how many pages there are.
if (isset($_GET['np'])) { // Already been determined.
$num_pages = $_GET['np'];
} else { // Need to determine.
// Count the number of records
$query = "SELECT COUNT(*) FROM retailers ORDER BY ret_name ASC";
$result = mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);
$num_records = $row[0];
// Calculate the number of pages.
if ($num_records > $display) { // More than 1 page.
$num_pages = ceil ($num_records/$display);
} else {
$num_pages = 1;
}
}
// Determine where in the database to start returning results.
if (isset($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;}
// Make the links to other pages, if necessary.
if ($num_pages > 1) {
echo '<br /><p>';
// Determine what page the script is on.
$current_page = ($start/$display) + 1;
// If it's not the first page, make a Previous button.
if ($current_page != 1) {
echo '<a href="viewRetailers.php?s=' . ($start - $display) . '&np=' . $num_pages . '">Previous</a> ';
}
// Make all the numbered pages.
for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $current_page) {
echo '<a href="viewRetailers.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '">' . $i . '</a> ';
} else {
echo $i . ' ';
}
}
// If it's not the last page, make a Next button.
if ($current_page != $num_pages) {
echo '<a href="viewRetailers.php?s=' . ($start + $display) . '&np=' . $num_pages . '" class="navtext">Next</a>';
}
echo '</p>';
} // End of links section.
// Make the query:
$q = "SELECT ret_id,ret_name,ret_add1,ret_add2,ret_town,ret_county,ret_pcode,ret_phone,ret_email,ret_web FROM retailers ORDER BY ret_name ASC LIMIT $start, $display";
$result = mysql_query ($q); // Run the query.Define the $result variable and display or send error message
// Table header
echo '<table align="left" cellpadding="5" cellpadding="5" class="maincopy" border="1">
<tr>
<td align="left"><strong>Edit</strong></td>
<td align="left"><strong>Delete</strong></td>
<td align="left"><strong>Shop Name</strong></td>
<td align="left"><strong>Shop Address & Details</strong></td>
</tr>
';
// Fetch and print all records.
while ($row = mysql_fetch_array($result)) {
echo '<tr valign="top">
<td align=left><a href="editRetailer.php?id=' . $row['ret_id'] . '">Edit</a></td>
<td align="left"><a href="deleteRetailer.php?id=' . $row['ret_id'] . '">Delete</a></td>
<td align="left"><h2>' . $row['ret_name'] . '</h2></td>
<td align="left">
' . $row['ret_add1'] . ' <br />
' . $row['ret_add2'] . ' <br />
' . $row['ret_town'] . ' <br />
' . $row['ret_county'] . ' <br />
' . $row['ret_pcode'] . ' <br />
' . $row['ret_phone'] . ' <br />
' . $row['ret_email'] . ' <br />
' . $row['ret_web'] . ' <br />
</td>
</tr>
';
}
echo '</table>';
mysql_close($dbc);
unset($dbc);
include ('includes/footer.html'); // Include the HTML footer.
I adapted this code from various sources but mainly from Larry Ulmans books.