 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
08-17-2008, 09:14 PM
|
#1 (permalink)
|
|
The Addict
Join Date: Aug 2008
Posts: 336
Thanks: 8
|
How can I track errors from a called function?
I am not sure if this goes here but I am kinda beginning with PHP so I put it here, even though it might be a problem in a different level.
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 Code:
<?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 Code:
<?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?
|
|
|
|
08-18-2008, 04:18 AM
|
#3 (permalink)
|
|
The Addict
Join Date: Aug 2008
Posts: 336
Thanks: 8
|
Oh they keep track of all the calls. That is more handy then the mess I've been trying to get. I'm going to incorporate it in the script and post the results, thanks.
|
|
|
|
08-18-2008, 07:04 PM
|
#4 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
|
Or, there is also the Reflection class (php 5+). It provides you waaay more info than that little function.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
|
|
|
|
08-18-2008, 08:04 PM
|
#5 (permalink)
|
|
is cute and cuddly
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
|
php 5.1.3 and above, to be exact, as I found out just recently.
*lurvs Reflection*

|
|
|
|
08-18-2008, 08:19 PM
|
#6 (permalink)
|
|
The Addict
Join Date: Aug 2008
Posts: 336
Thanks: 8
|
wow, i should have read the whole oop section. this is good stuff. going to look at it.
Thanks
|
|
|
|
08-18-2008, 09:28 PM
|
#7 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
|
Quote:
Originally Posted by xenon
Or, there is also the Reflection class (php 5+). It provides you waaay more info than that little function.
|
Reflection does not provide the information he want in this case because it does not provide a stack trace like debug_backtrace().
Combining a custom error handler set though set_error_handler() with debug_backtrace() makes it possible to show where the error occured where Reflection only can be used to extend the information from debug_backtrace() =)
(That is if I understand his problem correctly ofcourse)
__________________
|
|
|
08-18-2008, 11:39 PM
|
#8 (permalink)
|
|
is cute and cuddly
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
|
No Kalle. You are not allowed to speak of such things. Reflection can do anything. ANYTHING.
|
|
|
|
08-19-2008, 12:22 AM
|
#9 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
|
Quote:
Originally Posted by Kalle
Reflection does not provide the information he want in this case because it does not provide a stack trace like debug_backtrace().
|
Oh yes, it does. You just don't know about it :P
Try the getTrace method for an array containg all the trace info you need, and format it as you need, or, use the built-in getTraceAsString method, to get a default trace format. You can, of course, use something like this on the output:
PHP Code:
try { // ... } catch(Exception $e) { echo str_replace("\n", '<br />', $e->getTraceAsString()); }
to get a perfectly readable and displayable HTML form of the stack trace. Awesome, right? :D
The advantage of the exceptions is that you don't need to log the line on which an exception occured, nor the file in which the source code of the function/method was located. The exceptional engine keeps in mind all that stuff (and this is just the beginning of it) :) All you need to do is nicely ask for it.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
|
|
|
|
08-19-2008, 10:55 AM
|
#10 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
|
Quote:
Originally Posted by xenon
Oh yes, it does. You just don't know about it :P
Try the getTrace method for an array containg all the trace info you need, and format it as you need, or, use the built-in getTraceAsString method, to get a default trace format. You can, of course, use something like this on the output:
PHP Code:
try
{
// ...
}
catch(Exception $e)
{
echo str_replace("\n", '<br />', $e->getTraceAsString());
}
to get a perfectly readable and displayable HTML form of the stack trace. Awesome, right? :D
The advantage of the exceptions is that you don't need to log the line on which an exception occured, nor the file in which the source code of the function/method was located. The exceptional engine keeps in mind all that stuff (and this is just the beginning of it) :) All you need to do is nicely ask for it.
|
Then its not Reflection, but Exceptions =)
__________________
|
|
|
08-19-2008, 07:34 PM
|
#11 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
|
Exceptional code (to be read code that throws exceptions) + reflection = the greatest debugging tool ever (and even documentation, why not). As someone said already, you can do just about anything with reflection.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
|
|
|
|
08-19-2008, 08:02 PM
|
#12 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Quote:
Originally Posted by xenon
As someone said already, you can do just about anything with reflection.
|
Except slice my bread. I wish someone would invent a tool to do that! 
|
|
|
|
08-19-2008, 09:36 PM
|
#13 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
|
Except that, of course. And going to work instead of you. That's reflection weak points. But I'm sure the guys over at php.net work hardly on these issues 
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
|
|
|
|
08-19-2008, 10:34 PM
|
#14 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
|
Quote:
Originally Posted by xenon
Except that, of course. And going to work instead of you. That's reflection weak points. But I'm sure the guys over at php.net work hardly on these issues 
|
Well you will be glad to hear that Reflection has got some new methods to play around with in 5.3, most of the work atm at php.net is going towards the 5.3 release :)
__________________
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|