We have made some adjustments of course... but here is the class with everything including the comments as they stand now...
Code: Select all
<?php
<?php
//FunctionComment
//Describe what the function does
//***************************************************************************
//* Argument(Req/Opt) Basic Description *
//***************************************************************************
//* $argument(req/opt) What it does in the function *
//***************************************************************************
//* Variables Variable Definitions *
//***************************************************************************
//* $variable name What it does in the function *
//***************************************************************************
//ClassVariablesComment
//***************************************************************************
//* Class Variables *
//***************************************************************************
//* $variable name What that variable is used for *
//***************************************************************************
class dbConnect
{
//ClassVariablesComment
//***************************************************************************
//* Class Variables *
//***************************************************************************
//* $link This holds the database link information. *
//* $errors This array holds all of the error messages. *
//* $debug This chooses whether or not to send debug *
//* information. *
//***************************************************************************
var $link;
var $errors = array();
var $debug = false;
function dbConnect( $name="", $pass="", $db="", $host="localhost")
{
//FunctionComment
//This function is set up to instantiate a database connection
if( $name == "" || $pass == "" || $db == "" )
{
$this->setError("You need a password, username or database in order to use this class");
Return false;
}
$this->connect( $host, $name, $pass, $db ) or die( $this->getError() );
Return true;
}
function connect( $host, $name, $pass, $db )
{
//FunctionComment
//Connect is called to connect to a database. You will call this,
//most likely, once per class. It will return false if it has a problem
//connecting to the host and/or database. True means peachy. If there
//are any errors, check getErrors() to see what happened.
//***************************************************************************
//* Argument(Req/Opt) Basic Description *
//***************************************************************************
//* $host(req) Address of system with database *
//* $name(req) Username *
//* $pass(req) Password *
//* $db (req) Name of database to use *
//***************************************************************************
//* Variables Variable Definitions *
//***************************************************************************
//* $link Database Link (see Class Variables) *
//* $this->setError Refers to whatever instantiation is *
//* calling it, and sets the Error message. *
//***************************************************************************
//Standard connect to the mysql host. On error, it will call the if
//statement. If it connects, it will skip past the if.
$link = mysql_connect( $host, $name, $pass);
if(! $link )
{
//Error connecting to the host. Call getError to read about
//the error
$this->setError("Couldn't connect to database server");
Return false;
}
//This will try connecting to the database in question. This is just
//checking to make sure the specific database is there. On error it
//will call the if statement.
if(! mysql_select_db( $db, $link ))
{
//Error connecting to the database. Call getError to read about
//the error
$this->setError("Couldn't select database: $db");
Return false;
}
//This assumes that the statement worked just fine. The mySql link
//is saved and it returns true as the connect worked fine.
$this->link = $link;
Return true;
}
function getError()
{
//FunctionComment
//This function will return the last error that occurred. It will return this
//as a string and you can get it and read it to your hearts content.
//***************************************************************************
//* Argument(Req/Opt) Basic Description *
//***************************************************************************
//* N/A No arguments are req'd *
//***************************************************************************
//* Variables Variable Definitions *
//***************************************************************************
//* $this->errors Refers to whatever the errors array *
//* and returns the very last entry in it. *
//***************************************************************************
//Return from the errors array...[It counts the total number of elements in
//the errors array and then subtracts 1 because arrays start at 0 and
//counting starts at 1.
Return $this->errors[count($this->errors)-1];
}
function setError( $str )
{
//FunctionComment
//This function will add an error message to the end of the errors array.
//***************************************************************************
//* Argument(Req/Opt) Basic Description *
//***************************************************************************
//* $str(req) holds the error message *
//***************************************************************************
//* Variables Variable Definitions *
//***************************************************************************
//* $this->errors Adds an error message to the errors array. *
//***************************************************************************
//This is the same as errors[]=$str. It's an easier read for class objects.
array_push( $this->errors, $str );
}
function _query( $query )
{
//FunctionComment
//This function actually runs the query and will return the result back
//to the calling function.
//***************************************************************************
//* Argument(Req/Opt) Basic Description *
//***************************************************************************
//* $query(req) SQL Query.(Class calls this function) *
//***************************************************************************
//* Variables Variable Definitions *
//***************************************************************************
//* $this->setError Calls the setError function to add an *
//* error message. *
//***************************************************************************
//Start out by checking to see if the connection to the db is good.
if( ! $this->link )
{
//If the connection is not good, this error is added.
$this->setError("No active db connection");
Return false;
}
//This is running the query and returning the results. This is different
//for a select & an insert. But you don't need to worry about that.
$result = mysql_query( $query, $this->link );
if( ! $result )
{
//This assumes that the query had an error and that message is added
//to the errors array.
$this->setError("error: ".mysql_error());
Return false;
}
//This assumes that the connection & query are good and returns the results.
Return $result;
}
function setQuery( $query )
{
//FunctionComment
//This is in reference to a set query(like an insert or update). It will return
//the number of affected rows.
//***************************************************************************
//* Argument(Req/Opt) Basic Description *
//***************************************************************************
//* $query(req) SQL Query.(Probably an insert or update) *
//***************************************************************************
//* Variables Variable Definitions *
//***************************************************************************
//* $this->link DB connection link. Used to show number *
//* of affected rows. *
//***************************************************************************
if( ! $result = $this->_query( $query ) )
{
//This will add the error message inside of the _query function
//and will return false so that you will get false in your program.
Return false;
}
//Everything went just fine and the number of affected rows will be returned.
Return mysql_affected_rows( $this->link );
}
function getQuery( $query )
{
//FunctionComment
//This function is for a query that is getting some sort of data(like a select).
//It will return the information that you requested or false if there is an error.
//***************************************************************************
//* Argument(Req/Opt) Basic Description *
//***************************************************************************
//* $query(req) SQL Query.(Probably a Select Query) *
//***************************************************************************
//* Variables Variable Definitions *
//***************************************************************************
//* $this->_query Calls _query function, which runs the qry. *
//* $ret[] This holds the output of the query. *
//* $row Temp. variable to store values. *
//***************************************************************************
//This runs the query in the function _query and returns false if in error.
if( ! $result = $this->_query( $query ))
{
//This will add the error message inside of the _query function
//and will return false so that you will get false in your program.
Return false;
}
//If it gets this far, the query is working just fine and now we need
//somewhere to store the information it is returning.
$ret = array();
while( $row = mysql_fetch_assoc( $result ) )
{
$ret[] = $row;
}
Return $ret;
}
function getResource()
{
//FunctionComment
//This looks like it would be used to view the link to the mySql DB.
//***************************************************************************
//* Argument(Req/Opt) Basic Description *
//***************************************************************************
//* N/A *
//***************************************************************************
//* Variables Variable Definitions *
//***************************************************************************
//* $this->link DB connection link. *
//***************************************************************************
Return $this->link;
}
function select( $table, $condition="", $sort="" )
{
//FunctionComment
//This function is used to make the select statement easier. Just add the
//table, condition, & sort by.
//***************************************************************************
//* Argument(Req/Opt) Basic Description *
//***************************************************************************
//* $table(req) Name of the table you want to access *
//* $condition(opt) Where statements *
//* $sort(opt) How you want the info sorted *
//***************************************************************************
//* Variables Variable Definitions *
//***************************************************************************
//* $this->_makeWhereList Used to create the Where part of query. *
//* $query SQL query that is being made. *
//* $this->getQuery Calls getQuery to connect to DB. *
//* $error I'm not sure how this is used???? *
//***************************************************************************
//Creates the query. Calls _makeWhereList if there are any Where
//requirements($condition(s)). And sends the query back, if Debug
//is on.
/*I have no idea how it calls getQuery and sends 2 args?????????*/
$query = "SELECT * FROM $table";
$query .= $this->_makeWhereList( $condition );
if( $sort != "" )
{
$query .= " order by $sort";
}
$this->debug( $query );
Return $this->getQuery( $query, $error );
}
function insert( $table, $add_array )
{
//FunctionComment
//This function sets up the insert query for the database class
//***************************************************************************
//* Argument(Req/Opt) Basic Description *
//***************************************************************************
//* $table(req) Name of the table you want to access *
//* $add_array(req) Values to be inserted into table *
//***************************************************************************
//* Variables Variable Definitions *
//***************************************************************************
//* $this->_quote_vals Used to add ' ' if the value isn't a num. *
//* $keys Holds the keys [X] for the array. *
//* $values Holds the values array[X]= for the array. *
//* $query SQL query. *
//* setQuery Called for insert/update queries. *
//***************************************************************************
//Runs the array through the _quote_vals and add's '' if the values are
//not integers. Then it pulls the keys and the values to be placed
//into the query statement. The query is returned in debug mode. It
//will be sent to setQuery, to be run, whether debug is on/off.
$add_array = $this->_quote_vals( $add_array );
$keys = "(".implode( array_keys( $add_array ), ", ").")";
$values = "values (".implode( array_values( $add_array ),", ").")";
$query = "INSERT INTO $table $keys $values";
$this->debug( $query );
Return $this->setQuery( $query );
}
function update( $table, $update_array, $condition="" )
{
//FunctionComment
//This function sets up the update query for the database class
//***************************************************************************
//* Argument(Req/Opt) Basic Description *
//***************************************************************************
//* $table(req) Name of the table you want to access *
//* $update_array(req) Values to be inserted into table *
//* $condition(opt) Where values would go here. *
//***************************************************************************
//* Variables Variable Definitions *
//***************************************************************************
//* $update_pairs Used to hold completed fieldname/values *
//* $query Holds the SQL query as it's being written *
//* $update_array Initially holds the fieldnames & values. *
//***************************************************************************
//Initializes the update_pairs variable as an array
$update_pairs = array();
//checks out the update_array and stores it as something I don't know
foreach($update_array as $field=>$val)
{
//adds the value at the end of the update_pairs array
array_push( $update_pairs, "$field=".$this->_quote_val( $val ));
}
//sets up the Update query, running _makeWhereList if there are any
//Where conditions($condition) and sends the query back if debug is on.
//This will return false in error and the number of rows changed if it works.
$query = "UPDATE $table set ";
$query .= implode( ", ", $update_pairs );
$query .=$this->_makeWhereList( $condition );
$this->debug( $query );
Return $this->setQuery( $query );
}
function delete( $table, $condition="")
{
//FunctionComment
//This function sets up the delete query for the database class. It deletes
//from a table. This does not delete a table.
//***************************************************************************
//* Argument(Req/Opt) Basic Description *
//***************************************************************************
//* $table(req) Name of the table where you want to delete *
//* $condition(opt) Any conditions to the delete *
//***************************************************************************
//* Variables Variable Definitions *
//***************************************************************************
//* $query Holds the SQL query as it's being written *
//***************************************************************************
//sets up the Delete query running _makeWhereList if conditions exist,
//echoing the query if debug is on, and doing something crazy with setQuery.
$query = "DELETE FROM $table";
$query .= $this->_makeWhereList( $condition );
$this->debug( $query );
Return $this->setQuery( $query, $error );
}
function _makeWhereList( $condition )
{
//FunctionComment
//This function sets up the "Where section of a SQL Query. I don't really
//know how this works, exactly. You pass in the $condition array but I don't
//know how that array needs to look.
//***************************************************************************
//* Argument(Req/Opt) Basic Description *
//***************************************************************************
//* $condition(opt) Sends the $condition variable from the *
//* queries *
//***************************************************************************
//* Variables Variable Definitions *
//***************************************************************************
//* $query Holds the SQL query as it's being written *
//***************************************************************************
//If there is no condition, it will be empty and this function will end.
if( empty( $condition ))
{
Return "";
}
//Starts out the Where part of the SQL query.
$retstr = " WHERE ";
//If there are multiple conditions, it will be in an array. This will
//break them down to single statements.
if( is_array( $condition ))
{
$cond_pairs = array();
foreach($condition as $field=>$val)
{
array_push( $cond_pairs, "$field=".$this->_quote_val( $val ));
$retstr .= implode( " AND ", $cond_pairs );
}
}
//This means that the $condition isn't an array. We find out if it's a
//string & double check that it isn't empty
elseif( is_string( $condition ) && !empty( $condition ) )
{
//it's a string, so we just add it to the end of the Where.
$retstr .= $condition;
}
//We return the Where statement we created
Return $retstr;
}
function _quote_val( $val )
{
//FunctionComment
//This function sets up the "Where section of a SQL Query. I don't really
//know how this works, exactly. You pass in the $condition array but I don't
//know how that array needs to look.
//***************************************************************************
//* Argument(Req/Opt) Basic Description *
//***************************************************************************
//* $val(opt) Sends the $condition variable from the *
//* queries *
//***************************************************************************
//* Variables Variable Definitions *
//***************************************************************************
//* $query Holds the SQL query as it's being written *
//***************************************************************************
if( is_numeric( $val ))
{
Return $val;
}
Return "'".addslashes( $val )."'";
}
function _quote_vals( $array )
{
foreach( $array as $key=>$val)
{
$ret[$key] = $this->_quote_val( $val );
}
Return $ret;
}
function setDebug( $debugStatus )
{
$this->debug=$debugStatus;
}
function debug( $msg )
{
if( $this->debug )
{
print("$msg<br />");
}
}
}
?>
?>
Hope that helps...