Debugging
For debugging purposes I found it helpful to append messages from the script to a file (
debug.txt) like this:
<?php
$debug_file = "debug.txt";
$debug_filehandle = fopen($debug_file, "a");
fwrite($debug_filehandle, "\n\nNew execution...\n");
[snip]
fwrite($debug_filehandle, "Message xxxx etc.\n");
[snip]
?>
I created this simple script to give myself a quick way of checking the contents of the file:
<?php
// Display preamble HTML
echo "<html><head><title>Showing Contents of debug.txt";
echo" </title></head><body><pre>";
echo "<b><u>Showing Contents of debug.txt";
echo "</u></b>\n";
echo "...generated on ".date('l dS \of F Y h:i:s A')."\n\n";
$debug_file = "debug.txt";
$debug_filehandle = fopen($debug_file, "r");
rewind($debug_filehandle);
fpassthru($debug_filehandle);
// Display ending HTML
echo "</pre></body></head>";
?>
I also created this extended version of the
expirydataviewer to give myself a quick way of checking the contents of the three MySQL tables:
<?php
/*
// EXPIRY DATA VIEWER - customised
*/
function connect_to_db() {
if ($db = @mysql_connect("mysql.lsbu.ac.uk", "bushm", "****")) {
if (@mysql_select_db("bushm", $db)) {
return $db;
} else {
die( "<h3>Failed to obtain access to database.</h3>");
}
} else {
die( "<h3>Failed to connect to database server.</h3>");
}
}
//================================================================
/*
// MAIN PROGRAM
*/
// Display preamble HTML
echo "<html><head><title>Expiry Checker: ";
echo "Martin's Test Version.";
echo" </title></head><body><pre>";
echo "<b><u>Expiry Checker: ";
echo "Martin's Test Version.";
echo "</u></b>\n";
echo "...generated on ".date('l dS \of F Y h:i:s A')."\n\n";
$db = connect_to_db();
// in case the expirydataLIVE table doesn't exist
$sql = "SHOW COLUMNS FROM expirydataLIVE";
$result = mysql_query($sql, $db);
if ($result == FALSE) {
echo "\nContents of expirydataLIVE... DOES NOT EXIST!\n\n";
} else {
// in case the expirydataLIVE table contains no data
$sql = "SELECT * FROM expirydataLIVE";
$result = mysql_query($sql, $db);
if (mysql_num_rows($result) == 0) {
echo "\nContents of expirydataLIVE... EMPTY!\n\n";
} else {
echo "\nContents of expirydataLIVE...\n\n";
while ($expired_page = mysql_fetch_row($result)) {
$url = trim($expired_page[1]);
$expirydate = trim($expired_page[2]);
$owner = trim($expired_page[3]);
$message = trim($expired_page[4]);
echo "<a href=\"";
echo $url;
echo "\">";
echo $url;
echo "</a>";
echo "\nOwner = ";
echo "<a href=\"mailto:";
echo $owner;
echo "\">";
echo $owner;
echo "</a>";
echo ", Expired = ";
echo $expirydate;
// echo "\nMessage = ";
// echo $message;
echo "\n\n";
}
}
}
// in case the expirydataTEMP table doesn't exist
$sql = "SHOW COLUMNS FROM expirydataTEMP";
$result = mysql_query($sql, $db);
if ($result == FALSE) {
echo "\nContents of expirydataTEMP... DOES NOT EXIST!\n\n";
} else {
// in case the expirydataTEMP table contains no data
$sql = "SELECT * FROM expirydataTEMP";
$result = mysql_query($sql, $db);
if (mysql_num_rows($result) == 0) {
echo "\nContents of expirydataTEMP... EMPTY!\n\n";
} else {
echo "\nContents of expirydataTEMP...\n\n";
while ($expired_page = mysql_fetch_row($result)) {
$url = trim($expired_page[1]);
$expirydate = trim($expired_page[2]);
$owner = trim($expired_page[3]);
$message = trim($expired_page[4]);
echo "<a href=\"";
echo $url;
echo "\">";
echo $url;
echo "</a>";
echo "\nOwner = ";
echo "<a href=\"mailto:";
echo $owner;
echo "\">";
echo $owner;
echo "</a>";
echo ", Expired = ";
echo $expirydate;
// echo "\nMessage = ";
// echo $message;
echo "\n\n";
}
}
}
// in case the expirydataLASTWEEK table doesn't exist
$sql = "SHOW COLUMNS FROM expirydataLASTWEEK";
$result = mysql_query($sql, $db);
if ($result == FALSE) {
echo "\nContents of expirydataLASTWEEK... DOES NOT EXIST!\n\n";
} else {
// in case the expirydataLASTWEEK table contains no data
$sql = "SELECT * FROM expirydataLASTWEEK";
$result = mysql_query($sql, $db);
if (mysql_num_rows($result) == 0) {
echo "\nContents of expirydataLASTWEEK... EMPTY!\n\n";
} else {
echo "\nContents of expirydataLASTWEEK...\n\n";
while ($expired_page = mysql_fetch_row($result)) {
$url = trim($expired_page[1]);
$expirydate = trim($expired_page[2]);
$owner = trim($expired_page[3]);
$message = trim($expired_page[4]);
echo "<a href=\"";
echo $url;
echo "\">";
echo $url;
echo "</a>";
echo "\nOwner = ";
echo "<a href=\"mailto:";
echo $owner;
echo "\">";
echo $owner;
echo "</a>";
echo ", Expired = ";
echo $expirydate;
// echo "\nMessage = ";
// echo $message;
echo "\n\n";
}
}
}
// Display ending HTML
echo "</pre></body></head>";
?>