Index: /trunk/htdocs/index.php
===================================================================
--- /trunk/htdocs/index.php	(revision 218)
+++ /trunk/htdocs/index.php	(revision 219)
@@ -109,7 +109,11 @@
 include_once("constants.php");
 include_once("security.php");
-include_once("database.php");
+// // // include_once("database.php");
+require_once 'MysqlDB.php';
 include_once("date.php");
 require_once 'logger.php';
+include_once("header.html");
+
+try {
 
 
@@ -121,21 +125,23 @@
 $all_others_id = "all_others";
 
-$link = db_connect();
-
-if(!$link){
-  include_once("header.html");
-
-  echo"<body class=\"atoa\">\n\n";
-  
-  echo "<h1>ATNF Pulsar Data Archive</h1>\n\n";
-
-  echo "<b>\n The Archive is currently down for maintenance. <br>\n Please call again soon!. <br>\n</b>\n\n";
-
-  include_once("footer.html");
-  
-  echo "\n</body>\n</html>\n";
-  
-  exit;
- }
+//$link = db_connect();
+$db = new MysqlDB();
+$db->connect( DB_SERVER, DB_USERNAME, DB_PASS, DB_NAME );
+
+// // // if(!$link){
+// // //   include_once("header.html");
+// // // 
+// // //   echo"<body class=\"atoa\">\n\n";
+// // //   
+// // //   echo "<h1>ATNF Pulsar Data Archive</h1>\n\n";
+// // // 
+// // //   echo "<b>\n The Archive is currently down for maintenance. <br>\n Please call again soon!. <br>\n</b>\n\n";
+// // // 
+// // //   include_once("footer.html");
+// // //   
+// // //   echo "\n</body>\n</html>\n";
+// // //   
+// // //   exit;
+// // //  }
 
 //$link = db_connect_admin();
@@ -150,11 +156,19 @@
 // Find out the first and last dates
 //$query = "select min(mjd), max(mjd) from ".OBSERVATIONS_TABLE;
-$query = "select min(t.mjd), max(t.mjd) from ((select mjd from ".S70_TABLE.") union (select mjd from ".OBSERVATIONS_TABLE.")) as t";
-$result = mysql_query($query) or die('Query 1 failed: ' . mysql_error());
-
-$row = mysql_fetch_array($result);
-
-$min_mjd = $row[0];
-$max_mjd = $row[1];
+$query = "select min(t.mjd) as min, max(t.mjd) as max from ((select mjd from ".S70_TABLE.") union (select mjd from ".OBSERVATIONS_TABLE.")) as t";
+
+// // // $result = mysql_query($query) or die('Query 1 failed: ' . mysql_error());
+// // // 
+// // // $row = mysql_fetch_array($result);
+// // // 
+// // // $min_mjd = $row[0];
+// // // $max_mjd = $row[1];
+
+$db->DoQuery( $query );
+$db->EnsureNumRows( 'equals', 1 );
+$row = $db->FetchRowAsArray();
+
+$min_mjd = $row['min'];
+$max_mjd = $row['max'];
 
 // Convert from MJD to JD
@@ -185,5 +199,5 @@
 
 // Free resultset
-mysql_free_result($result);
+// // // mysql_free_result($result);
 
 // Clear any old form inputs in session cache
@@ -193,14 +207,23 @@
 // Get the field names to fill form checkboxes
 
-$query = "SHOW COLUMNS FROM ".OBSERVATIONS_TABLE;
-$result = mysql_query($query) or die ('Query 2 failed: ' . mysql_error());
-
-$fields = array();
-if (mysql_num_rows($result)) {
-	while($row = mysql_fetch_assoc($result)) {
-	  if(($row['Field'] != "data_loc") && ($row['Field'] != "file_size_bytes"))
-	    array_push($fields, $row);
-	}
-}
+// // // $query = "SHOW COLUMNS FROM ".OBSERVATIONS_TABLE;
+// // // $result = mysql_query($query) or die ('Query 2 failed: ' . mysql_error());
+// // // 
+// // // $fields = array();
+// // // if (mysql_num_rows($result)) {
+// // // 	while($row = mysql_fetch_assoc($result)) {
+// // // 	  if(($row['Field'] != "data_loc") && ($row['Field'] != "file_size_bytes"))
+// // // 	    array_push($fields, $row);
+// // // 	}
+// // // }
+
+$db->DoQuery( 'DESCRIBE ' . OBSERVATIONS_TABLE );
+$fields = $db->GetLastResult();
+
+$new_fields = Array();
+foreach( $fields as $next_row )
+  if( $next_row['Field'] != 'data_loc' && $next_row['Field'] != 'file_size_bytes' )
+    $new_fields[] = $next_row;
+$fields = $new_fields;
 
 $field_names = Array("filename" => "Filename",
@@ -247,23 +270,27 @@
 //$query = "select distinct data_type from " . OBSERVATIONS_TABLE . " order by data_type";
 $query = "(select distinct data_type from ".OBSERVATIONS_TABLE.") union (select distinct data_type from ".S70_TABLE.")";
-$result = mysql_query($query) or die ('Query 3 failed: ' . mysql_error());
-
-$data_types = array();
-
-if (mysql_num_rows($result)) {
-	while($row = mysql_fetch_array($result, MYSQL_NUM)) {
-		array_push($data_types, $row[0]);
-	}
-}
+// // // $result = mysql_query($query) or die ('Query 3 failed: ' . mysql_error());
+// // // 
+// // // $data_types = array();
+// // // 
+// // // if (mysql_num_rows($result)) {
+// // //   while($row = mysql_fetch_array($result, MYSQL_NUM)) {
+// // //     array_push($data_types, $row[0]);
+// // //   }
+// // // }
+
+$db->DoQuery( $query );
+$data_types = $db->GetLastResult();
+
 
 ///////////////////////////////////////////////
 // If the user clicked on the logout button
 if (isset($HTTP_GET_VARS['command'])) {
-	$command = $HTTP_GET_VARS['command'];
-	if ($command == 'logout') {
-		//unset($_SESSION['auth_code']);
-		//unset($_SESSION['username']);
-		$_SESSION = array();
-	}
+  $command = $HTTP_GET_VARS['command'];
+  if ($command == 'logout') {
+    //unset($_SESSION['auth_code']);
+    //unset($_SESSION['username']);
+    $_SESSION = array();
+  }
 }
 
@@ -310,10 +337,9 @@
 // }
 
-include_once("header.html");
 
 
 // Initial state where no username or password supplied OR bad login state
 if ($auth_code != OK) { ?>
-	<body class="front_page">
+  <body class="front_page">
 
 
@@ -658,6 +684,6 @@
 				<?php
 				foreach ($data_types as $data_type) {
-					echo "
-					<option value=\"$data_type\"> $data_type </option>";
+					echo '
+					<option value="' . $data_type['data_type'] . '"> ' . $data_type['data_type'] . '</option>';
 				}
 				?>
@@ -726,5 +752,17 @@
 <div style="min-height: 20px; max-height: 1000px;"> </div>
 <?php 
-}?>
+}
+
+} catch( MysqlDBException $e ) {
+  logger_exception( $_SESSION['username'], $e->getCode, $e->getMessage(), '' );
+  echo '<p><div class="exception_box">' .
+       '<center>' .
+       '<b>A database error was encountered</b> <br>' .
+       'the details of the error have been logged, please contact the system administrator for futher assistance <br>' .
+       '</center>' .
+       '</div><p>';
+}
+
+?>
 </div>
 
@@ -756,5 +794,6 @@
 }
 
-mysql_close($link);
+// // // mysql_close($link);
+
 
 ?>
Index: /trunk/htdocs/query.php
===================================================================
--- /trunk/htdocs/query.php	(revision 218)
+++ /trunk/htdocs/query.php	(revision 219)
@@ -33,9 +33,15 @@
 include_once("header.html"); 
 
+try {
+//  
+// } catch( MysqlDBException $error ) {
+//   echo $error;
+// }
+
 echo "<body class=\"atoa\">\n";
 echo "    <!-- Start Content -->\n";
 
 include_once("constants.php");
-include_once("database.php");
+// // // include_once("database.php");
 include_once("utils.php");
 include_once("date.php");
@@ -43,4 +49,5 @@
 include_once("print_tables.php");
 require_once 'logger.php';
+require_once 'MysqlDB.php';
 define("PSR_SEARCH_RADIUS",0.5);
 
@@ -376,5 +383,5 @@
     //
 
-    $whereString = "";
+    $whereString = "1 = 1";
     $selectString = "SELECT ";
 
@@ -510,8 +517,7 @@
     //
 
-    $link = db_connect();
-
-    // Remove any leading or trailing AND and OR
-    $whereString = trim($whereString, " AND OR");
+    $db = new MysqlDB();
+    $db->connect( DB_SERVER, DB_USERNAME, DB_PASS, DB_NAME );
+// // //     $link = db_connect();
 
     $query = "( $selectString FROM ".S70_TABLE." WHERE $whereString ) UNION ( $selectString FROM ".OBSERVATIONS_TABLE." WHERE $whereString )";
@@ -523,31 +529,43 @@
     // get the number of results we would get without limiting it.
     // and give a warning if we're not returning all of them.
-    $numquery = "select a.num+b.num as num from (select count(*) as num from ".S70_TABLE." where $whereString) as a, (select count(*) as num from ".OBSERVATIONS_TABLE." where $whereString) as b";
-    $numresult = mysql_query($numquery);
-    $numrows = mysql_fetch_assoc($numresult);
-    if( ($numrows['num'] > MAX_RESULTS) && ($state=="results") ) {
-      $_SESSION['num_msg']= "<center>\n<b>Note:</b> Your query returns more results (".$numrows['num'].") than the internal limit.<br>\n".
-	"Only showing the first ".MAX_RESULTS." rows.\n</center><br>\n";
-    }
-    else{
-      $_SESSION['num_msg']= "";
-    }
-    mysql_free_result($numresult);
+    $num_query = "select a.num+b.num as num from (select count(*) as num from ".S70_TABLE." where $whereString) as a, (select count(*) as num from ".OBSERVATIONS_TABLE." where $whereString) as b";
+// // //     $numresult = mysql_query($numquery);
+// // //     $numrows = mysql_fetch_assoc($numresult);
+// // //     if( ($numrows['num'] > MAX_RESULTS) && ($state=="results") ) {
+// // //       $_SESSION['num_msg']= "<center>\n<b>Note:</b> Your query returns more results (".$numrows['num'].") than the internal limit.<br>\n".
+// // // 	"Only showing the first ".MAX_RESULTS." rows.\n</center><br>\n";
+// // //     }
+// // //     else{
+// // //       $_SESSION['num_msg']= "";
+// // //     }
+// // //     mysql_free_result($numresult);
+
+    $num_result = $db->DoQuery( $num_query );
+    $row = $db->FetchRowAsArray();
+    if( $row['num'] > MAX_RESULTS && $state == 'results' )
+      $_SESSION['num_msg'] =
+	   "<center>\n" .
+	   "<b>Note:</b> Your query returns more results (".$row['num'].") than the internal limit.<br>\n" .
+	   "Only showing the first ".MAX_RESULTS." rows.\n" .
+	   "</center><br>\n";
     
-    $result = mysql_query($query) or die('Main Query failed: ' . mysql_error() . "\n". $query);
-  
-    logger_query( $_SESSION['username'], $query, mysql_num_rows($result) );
-    
-    $all_results = array();
-
-    while ($line = mysql_fetch_assoc($result)) {
-      array_push($all_results, $line );
-    }
+// // //     $result = mysql_query($query) or die('Main Query failed: ' . mysql_error() . "\n". $query);
+
+    $result = $db->DoQuery($query);
+    $all_results = $db->GetLastResult();
+
+    logger_query( $_SESSION['username'], $query, $db->FetchNumRows() );
+
+// // //     $all_results = array();
+// // // 
+// // //     while ($line = mysql_fetch_assoc($result)) {
+// // //       array_push($all_results, $line );
+// // //     }
 	       
     $_SESSION['results'] = $all_results;
 	       
-    mysql_free_result($result);
-
-    mysql_close($link);
+// // //     mysql_free_result($result);
+// // // 
+// // //     mysql_close($link);
 
   }
@@ -746,5 +764,16 @@
 printNavigate($startRange, $num_display_rows);
 echo "</center>\n";
-	
+
+} catch( MysqlDBException $e ) {
+  logger_exception( $_SESSION['username'], $e->getCode, $e->getMessage(), '' );
+  echo '<p><div class="exception_box">' .
+      '<center>' .
+      '<b>A database error was encountered</b> <br>' .
+      'the details of the error have been logged, please contact the system administrator for futher assistance <br>' .
+      '</center>' .
+      '</div><p>';
+}
+
+
 echo "</form>\n";
 include("footer.html");
