General Question

iTony's avatar

How can I track errors inside a called function?

Asked by iTony (202points) August 17th, 2008

My problem is that i made a static query class to have more security in queries before sending them. But i want to know if there is an error in the query to tell me on which file on what line, the query method was called. like this:

<?php
class QUERY{
#properties
public static $Result;

#Queries
static function select($columns, $table, $fit=NULL, $match=NULL)
{
//make sure parameters are right type
(!is_string($table))? return false;
(!is_array($columns) || !is_string($columns))? return false;
(!is_string($fit) || !is_null($fit))? return false;
(!is_string($match) || !is_null($match))? return false;

(is_string($columns)? $columns=explode(”:”,$columns);

//security door
$table=quoteSmart($table);
$columns=quoteSmartArray($columns);
($fit!=NULL)? $fit=quoteSmart($fit);
($match!=NULL)? $match=quoteSmart($match);

//make a safe query
$query = sprintf(
(!is_array($columns)) return false;“SELECT %s FROM %s”,
implode(”, ”, $columns), $table);

if(!$fit==NULL && !$match==NULL)
$query .= sprintf(” WHERE %s=%s”, $fit, $match)

$sql=mysql_query($query); unset($query);
self::$Result=array();
while($row=mysql_fetch_assoc($sql)
or throw new Exception(“Error in Line 36 in Query class ”.mysql_error()))
{
for($i=0; $i<count($res);$i++)
{
foreach($row as $key => $value)
self::$Result[$i][$key]=$value;
}
}

return self::$Result;
}
}
?>

then in another file i can make a called like this:

<?php
$res=QUERY::select(‘CommentID:UserID:Text:DateTime:MediaID’,
‘comments’, ‘CommentID’, $id);

$ID=$res[0][‘CommentID’];

$UserID=$res[0][‘UserID’];

$MediaID=$res[0][‘MediaID’];

$Comment=$res[0][‘Comment’];

$Date=$res[0][‘DateTime’];
?>

as you can see the error is sent and gives me the error line of the query method instead of giving the the line and file where it was called. and I am trying to find a way of telling me that, but i haven’t and it’s frustrating me.
any advice or tips?

Observing members: 0 Composing members: 0

1 Answer

wgallios's avatar

Use exceptions

$query = “SELECT * FROM table”;

if(!$results = mysql_query($query))
{
throw new exception(mysql_error());
}

then you can use a try catch on that function

——-

try{
QUERY::select();
}catch(Exception $e){
echo $e;
}

that exception will contain the entire stack trace to tell you where the error occurred. Hope that helps.

Answer this question

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther