<?PHP
/***************************************************************************
* MySql.php
*
* begin : June 2004
* version : 27 July 2006
* copyright : (C) 2004,2006 grandolini.net
*
* MySql database connector
*
***************************************************************************/
if(is_callable(db_connect)) return;
if(!function_exists('db_connect'))
{
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Connect to the database
#
function db_connect($args=array()) { $exec=@mysql_connect($args[0],$args[1],$args[2]); if(!$exec) { die(db_err()); } else { return($exec); } }
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Selects the database to use
#
function db_select_db($args=array())
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
if(isset($args[1]))
{
$exec=@mysql_select_db($args[0], $args[1]); if(!$exec) { die(db_err()); } else { return($exec); }
}
$exec=@mysql_select_db($args[0]); if(!$exec) { die(db_err()); } else { return($exec); }
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Executes a query
#
function db_query($query, $flag='')
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
global $dbh;
$exec[0]=@mysql_query($query, $dbh); # resource ID of the query
if(!$exec or $exec[0]=='' and $flag=='')
{
echo(BuildMySqlString('Query failed on database: <strong>'.$dbh.'</strong><hr /><font style="color: black; background-color: yellow; ">'.$query.'</font><hr />'));
exit;
}
else
{
if(substr($exec[0],0,8)=='Resource')
{
# if the query includes a LIMIT parameter, $exec[0] is set to that limit
# otherwise to the Resource ID that can be used to get the number of records found
#
$exec[1]=@mysql_num_rows($exec[0]); # number of records found
}
return($exec);
}
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Fetches records -- set $debug to print rows
# Output: array of records
#
function db_fetch($query_id,$debug=false)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
$rows=array(); $i=0; $ii=0;
#
# Get record by record
#
while($row=@mysql_fetch_array($query_id,MYSQL_ASSOC))
{
# Build the output array
#
array_push($rows,$row);
if($debug)
{
# Load array data
#
unset($tmp_array);
$tmp_array=$rows[$i];
$fields_record=count($tmp_array); # number of fields per record
reset($tmp_array); # set to the first array field
while($tmp_next=each($tmp_array)) { $labels[$ii]=$tmp_next['key']; $table_field[$ii]=$tmp_next['value']; $ii++; }
$i++;
}
}
if($debug)
{
?><br /><?php if(!isset($labels)) { $labels[0]='No records in this table!'; $table_field[0]=$labels[0]; $fields_record=0; }
ShowTable($labels,$table_field,$fields_record,'80%','center',true,'A');
}
return($rows);
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Returns error number
#
function db_err()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
return('<img src="img/warning.gif" border="0" /><br /><font style="color: black; background-color: yellow; ">MySQL Error ('hide@address.com().'): 'hide@address.com().'</font><hr><a href="'.$_SERVER[HTTP_HOST].'">Click here</a> to return to the main page<hr>');
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Prepare error string
#
function BuildMySqlString($temp)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
return('<img src="img/warning.gif" border="0" /><br /><font style="color: black; background-color: yellow; ">MySQL Error: '.$temp.'</font>');
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function db_close($dbh) { return @mysql_close($dbh); } # Close a database server connection
function db_last_id($dbh) { return @mysql_insert_id($dbh); } # Return the last generated RRN
function db_affected($dbh) { return @mysql_affected_rows($dbh); } # Return the number of affected rows
function db_free_result($query_id) { return(@mysql_free_result($query_id)); } # Destroy a query result to free memory
}
?>