<?
/* db.php
* This file contains database functions. You can set the options for
* DB_DIE_ON_FAIL and DB_DEBUG from your main program to affect the behavior
* when errors occur. The defaults are as follows:
*/
$DB_DIE_ON_FAIL = true; /* does script terminate if db call fails? */
$DB_DEBUG = true; /* provide detailed debugging messages? */
/* Functions listed in this file:
function db_connect($dbhost,$dbname,$dbuser,$dbpass)
result function db_query($query)
ass ary function db_nextarray($result)
array function db_nextrow($result)
value function db_nextvalue($result)
object function db_nextobject($result)
number function db_numrows($result)
id function db_insertid()
function db_disconnect()
html function db_listbox ($value_field,$label_field,$query,$default="")
function db_freeresult($result)
function db_handleError($detail)
*/
/* This function establishes a database connection with the specified
* parameters. If it fails, it will halt the script and display an
* error message.
*/
function db_connect(
$dbhost, /* hostname or ipaddress of db host (eg. "localhost") */
$dbname, /* name of the database */
$dbuser, /* user name to access database */
$dbpass /* password for user */
){
global $DB_DIE_ON_FAIL, $DB_DEBUG;
if (! mysql_pconnect($dbhost, $dbuser, $dbpass)) {
if ($DB_DEBUG) {
echo "<h2>Can't connect to $dbhost as $dbuser ($dbpass)</h2>";
echo "<p><b>MySQL Error</b>: ", mysql_error();
} else {
echo "<h2>Database error (couldn't connect)</h2>";
}
if ($DB_DIE_ON_FAIL) {
echo "<p>This script cannot continue, terminating.";
die();
}
}
if (! mysql_select_db($dbname)) {
if ($DB_DEBUG) {
echo "<h2>Can't select database $dbname</h2>";
echo "<p><b>MySQL Error</b>: ", mysql_error();
} else {
echo "<h2>Database error (cant't connect to $dbname)</h2>";
}
if ($DB_DIE_ON_FAIL) {
echo "<p>This script cannot continue, terminating.";
db_disconnect();
die();
}
}
}
/* This function attempts to execute a query, $query. If it fails, it will
* terminate the script with an error message and the query that was being
* executed. If it was successful, it will return a result ID.
*/
function db_query(
$query /* the SQL statement to send to the database */
){
global $DB_DIE_ON_FAIL, $DB_DEBUG;
// echo $query;
$result = mysql_query($query);
if (! $result) {
if ($DB_DEBUG) {
echo "<h2>Can't execute query</h2>";
echo "<pre>$query</pre>";
echo "<p><b>MySQL Error</b>: ", mysql_error();
} else {
echo "<h2>Database error (query error)</h2>";
}
if ($DB_DIE_ON_FAIL) {
echo "<p>This script cannot continue, terminating.";
db_disconnect();
die();
}
}
return $result;
}
/* This function returns an associative array containing the next row from
* the query specified by the result ID. If there is no more data, it returns
* FALSE.
*/
function db_nextarray(
$result /* the id returned from a successful db_query */
){
return mysql_fetch_array($result);
}
/* This function returns an array containing the next row from
* the query specified by the result ID. If there is no more data, it returns
* FALSE.
*/
function db_nextrow(
$result /* the id returned from a successful db_query */
){
return mysql_fetch_row($result);
}
/* This function returns a value with the first field from the next row from
* the query specified by the result ID. If there is no more data, it returns
* FALSE.
*/
function db_nextvalue(
$result /* the id returned from a successful db_query */
){
$array = mysql_fetch_row($result);
return $array[0];
}
/* This function returns an object with properties corresponding to the colums
* listed in a select query (specified by the result ID). If there is no more
* data, it returns FALSE.
*/
function db_nextobject(
$result /* the id returned from a successful db_query */
){
$obj = mysql_fetch_object($result);
return $obj;
}
/* This function returns the number of rows returned by the SELECT query
* specified by the result ID.
*/
function db_numrows(
$result=FALSE /* the id returned from a successful db_query */
){
if ($result === FALSE) {
return mysql_affected_rows();
} else {
return mysql_num_rows($result);
}
}
/* Returns the last ID of the last insert statement if any AUTO_INCREMENT
* or auto sequence field was in the table.
*/
function db_insertid() {
return mysql_insert_id();
}
/* This function closes the current database connection.
*/
function db_disconnect() {
mysql_close();
}
// Returns the a set of <option value...> tags from a db query
function db_listbox (
$value_field, // field to use for <option value=VALUE>label
$label_field, // field to use for <option value=value>LABEL
$query, // database query
$default="" // options with this value are selected by default, can be a scalar or a list
) {
$output = "";
$result = db_query($query);
while ($r = db_nextarray($result)) {
$value = $r[$value_field];
$label = $r[$label_field];
if (is_array($default))
$selected = empty($default[$value]) ? "" : "selected";
else
$selected = $value == $default ? "selected" : "";
$output .= "<option value='$value' $selected>$label\n";
}
return $output;
}
/* Release resources taken up by a query */
function db_freeresult($result) {
mysql_free_result($result);
}
/* display a db error message */
function db_handleError($detail) {
$msg = "$detail\n";
$msg = $detail."\n"."Mysql error : ".mysql_errno()." - ".mysql_error();
die ($msg);
}
?>