Page 1 of 1
Function has One Arg, What if I send two...?
Posted: Tue Mar 02, 2004 9:12 am
by randomblink
Ok. I was putting together a class out of a book, when I ran across the following...
The following code is a getQuery function used by the class. You will notice that this function has ONE arg. $query.
Code: Select all
<?php
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;
}
?>
Now then, if you look in the comments here, you will see that this select function calls the getQuery command with: Return $this->getQuery( $query, $error ); sending TWO arguments??? I am lost... Is this a mistake? Or does PHP do something with an extra arg...? Are we saving the value of this variable so it survives outside the function? The writer of the book does a POOR job explaining anything. I am trying to comment this class so I can understand it better, and this just floored me...
Code: Select all
<?php
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 );
}
?>
Any help would be appreciated...
Posted: Tue Mar 02, 2004 9:15 am
by markl999
As it stands it does look like a mistake. One possible way it could work is if $this->debug($query) sets $this->error and then it should be returning $this->getQuery( $query, $this->error );
Is the var 'error' mentioned anywhere else in the class (specifically in the debug method) ?
Here's the whole class...
Posted: Tue Mar 02, 2004 9:17 am
by randomblink
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...
Posted: Tue Mar 02, 2004 9:20 am
by markl999
Looks to me like $this->getQuery( $query, $error ); should be $this->getQuery( $query, $this->getError() );
Hmmm..
Posted: Tue Mar 02, 2004 9:23 am
by randomblink
Isn't that still sending the query two args?
I am just stumped as to why you would do such a thing?
I am rather new to php so... who knows...
Thanks for your help, I will try out your thoughts on the matter and see what happens... Thanks again...
On a side note, what does seem to happen is I get the SQL Query outputted to screen on calls that go bad, which is a little odd... Maybe it was planned, maybe not... Thanks again tho...
Posted: Tue Mar 02, 2004 9:31 am
by markl999
Sorry, you're right, i misread the code

It shouldn't be passing $error or $this->getError() at all, same goes for Return $this->setQuery( $query, $error );
$error is just a class var that's set using setError() and retrieved using getError() so it shouldn't be passed around in the member vars. So it just looks like it should be there at all in the set/getQuery calls.
Ah
Posted: Tue Mar 02, 2004 9:33 am
by randomblink
markl999 wrote:So it just looks like it should be there at all in the set/getQuery calls.
Is that a SHOULD or SHOULDN'T?
(I think you meant shouldn't but said should... please clarify)
Thanks alot man... If it SHOULDN'T be there, I am gonna remove it...
Posted: Tue Mar 02, 2004 9:35 am
by markl999
Yeah "shouldn't".
Sorry for the confusion, the brain hasn't fully kicked in yet
