I tried to comment aswell as I could. I am trying to learn to comment alot more than I am use to, hopefully I will get use to this. Ayhow let me know what you think.
<?php
/**
* @file : mysql.php
* @begin : Tuesday, September 06, 2005
* @email : support@phphorum.comm
* @copyright : Copyright (c) 2005 Timothy Hensley
* @version : $Id: mysql.php, v0.1 Tuesday, September 06, 2005 6:35pm $
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*/
/**
* Handles sql commands. Currently there are no other sql classes to support multiple databases
* such as postgreSQL, Oracle, or even MSSQL
*
* @package : sql
*/
class sql
{
/**#@+
* @access : public
* @var : NULL
*/
var $sql_link = NULL;
var $sql_db = NULL;
/**#@-*/
/**#@+
* @access : public
* @var : integer
*/
var $affected_rows = 0;
var $queries_used = 0;
/**#@-*/
/**
* Displays the errors that occur with an optional custom error message.
*
* @param : error_message : custom message used to describe error
* @package : sql_error
*/
function sql_error($error_message = "")
{
echo "<table width="100%" border="0" cellspacing="0" cellpadding="5">
<tr>
<td colspan=\"2\" align=\"right\">There appears to be an error. Below the information is provided.</td>
</tr>";
if ($error_message != "")
{
echo "<tr>
<td width=\"20%\" align=\"right\">Message: </td>
<td width=\"80%\" align=\"justify\">".$error_message."</td>
</tr>";
}else
{
$result = array();
$result['message'] = @mysql_error($this->sql_db);
$result['code'] = @mysql_errno($this->sql_db);
echo "<tr>
<td width=\"20%\" align=\"right\">Code: </td>
<td width=\"80%\" align=\"justify\">".$result['code']."</td>
</tr>
<tr>
<td width=\"20%\" align=\"right\">Message: </td>
<td width=\"80%\" align=\"justify\">".$result['message']."</td>
</tr>";
}
echo "</table>";
exit;
}
/**
* Creates an sql link, and establishs a connection to the database.
*
* @param : database_username : the database's username used to access the server
* @param : database_password : the username's password used to prove access
* @param : database_name : the database name in which access is gained
* @param : database_host : the server the database is located on
* @package : sql : class constructor
*/
function sql($database_username, $database_password, $database_name, $database_host = 'localhost')
{
if (($this->sql_link = @mysql_connect($database_host, $database_username, $database_password)) != FALSE)
{
if (($this->sql_db = @mysql_select_db($database_name, $this->sql_link)) != FALSE)
{
return $this->sql_db;
}else
{
return $this->sql_error('Unable to connect to database '.$database_name.'.');
}
}else
{
return $this->sql_error('Unable to establish mysql connection with user '.$database_username.'.');
}
}
/**
* Closes the current active sql connection
*
* @package : sql_close
*/
function sql_close()
{
if ($this->sql_db)
{
if ($this->query_result)
{
@mysql_free_result($this->query_result);
}
$result = @mysql_close($this->sql_db);
return $result;
}else
{
return false;
}
}
/**
* Executes a query, if it returns false, return an error.
*
* @param : query : if left empty an error is returned
* @package : sql_query
*/
function sql_query($query = "")
{
unset($this->query_result);
if ($query != "")
{
if (($this->query_result = @mysql_query($query, $this->sql_db)) != FALSE)
{
$this->queries_used++;
$this->affected_rows += mysql_affected_rows();
return $this->query_result;
}else
{
return $this->sql_error();
}
}else
{
return $this->sql_error('Query empty.');
}
}
/**
* Grabs the last executed query, and runs it with a num_rows function. The
* num_rows function will return the number of rows returned by the previous
* query. If a query is still unable to be found, returns FALSE.
*
* @param : query_id : if left NULL the previous sql operation is used
* @package : sql_numrows
*/
function sql_numrows($query_id = NULL)
{
if (!$query_id)
{
$query_id = $this->query_result;
}
if ($query_id)
{
$result = @mysql_num_rows($query_id);
return $result;
}else
{
return FALSE;
}
}
/**
* Grabs the last executed query, and runs it with a num_fields function. The
* num_fields will return the number of fields in a row returned based on the
* previous query. If a query is still unable to be found, returns FALSE.
*
* @param : query_id : if left NULL the previous sql operation is used
* @package : sql_numfields
*/
function sql_numfields($query_id = NULL)
{
if (!$query_id)
{
$query_id = $this->query_result;
}
if ($query_id)
{
$result = @mysql_num_fields($query_id);
return $result;
}else
{
return FALSE;
}
}
/**
* Grabs the last executed query, and runs it with a field_name function. The
* field_name function will return the name of a field via the field index. If
* a query is still unable to be found, returns FALSE.
*
* @param : offset : field index
* @param : query_id : if left NULL the previous sql operation is used
* @package : sql_fieldname
*/
function sql_fieldname($offset, $query_id = NULL)
{
if (!$query_id)
{
$query_id = $this->query_result;
}
if ($query_id)
{
$result = @mysql_field_name($query_id, $offset);
return $result;
}else
{
return FALSE;
}
}
/**
* Grabs the last executed query, and runs it with a fetch_assoc function. The
* fetch_assoc function will return rows from the database depending on the
* conditions specified in the query. If a query is still unable to be found,
* returns FALSE.
*
* @param : query_id : if left NULL the previous sql operation is used
* @package : sql_fetcharray
*/
function sql_fetcharray($query_id = NULL)
{
if (!$query_id)
{
$query_id = $this->query_result;
}
if ($query_id)
{
$result = @mysql_fetch_assoc($query_id);
return $result;
}else
{
return FALSE;
}
}
/**
* Grabs the last executed query, and runs it with a free_result function. The
* free_result function will free up memory used by the previous query. If a
* query is still unable to be found, returns FALSE.
*
* @param : query_id : if left NULL the previous sql operation is used
* @package : sql_freeresult
*/
function sql_freeresult($query_id = NULL)
{
if (!$query_id)
{
$query_id = $this->query_result;
}
if ($query_id)
{
@mysql_free_result($query_id);
return TRUE;
}else
{
return FALSE;
}
}
} // End of @package : sql
?>