JasonLewis Posted August 22, 2008 Share Posted August 22, 2008 I'm trying to create decent error reporting. Basically I run a query like this: $query = $DB->query("SELECT `blah` FROM `table` WHERE `foo`='bar'", err()); Now the second parameter for that function is calling another function which looks like this: function err(){ return array("file" => basename(__FILE__), "line" => __LINE__); } Which I thought would work. Because I want to say something along the lines of "Error was found in index.php on line 54". But it will only return the line that the return is on. Which, I guess, makes sense. Rather annoying though. Is there another way of going about this which doesn't go like this: $query = $DB->query("SELECT `blah` FROM `table` WHERE `foo`='bar'", array("file" => basename(__FILE__), "line" => __LINE__)); Suggestions welcome. Cheers. Link to comment https://forums.phpfreaks.com/topic/120826-decent-error-reporting/ Share on other sites More sharing options...
448191 Posted August 22, 2008 Share Posted August 22, 2008 You could do something like this (simplified): public function query($sql, Exception $e) { if($queryFailed) { throw $e; } } $db->query("SELECT `blah` FROM `table` WHERE `foo`='bar'", new Exception('Blahblah')); Link to comment https://forums.phpfreaks.com/topic/120826-decent-error-reporting/#findComment-622820 Share on other sites More sharing options...
JasonLewis Posted August 22, 2008 Author Share Posted August 22, 2008 Okay cheers that's working okay. I've done it like this: $query = $DB->query("SELECT `blah` FROM `table` WHERE `foo`='bar'", new Exception()); Then in my query function I'm just using $e->getLine() and $e->getFile(). Is that okay to do it like that? Link to comment https://forums.phpfreaks.com/topic/120826-decent-error-reporting/#findComment-622823 Share on other sites More sharing options...
448191 Posted August 22, 2008 Share Posted August 22, 2008 Is that okay to do it like that? I don't see WHY you would do that. Throwing the exception results in a fatal error. That's not what you want? Link to comment https://forums.phpfreaks.com/topic/120826-decent-error-reporting/#findComment-622830 Share on other sites More sharing options...
JasonLewis Posted August 22, 2008 Author Share Posted August 22, 2008 I'm not throwing the exception. I'm just making a new exception, then getting the line number and file name from it. die("MySQL Query failed.<br />MySQL said: ".mysql_error()."<br />Error was found in ".($err->getFile())." on line ".($err->getLine())); Where $err is the new Exception(). Link to comment https://forums.phpfreaks.com/topic/120826-decent-error-reporting/#findComment-622848 Share on other sites More sharing options...
448191 Posted August 22, 2008 Share Posted August 22, 2008 Sorry for being blunt, but that' s just stupid. Never, ever, ever call die() or exit() on error. You don't care for an error log? Just throw the exception. If you want, you can insert %s placeholders in the error message and use sprintf to insert the feedback from mysql_error(). Link to comment https://forums.phpfreaks.com/topic/120826-decent-error-reporting/#findComment-622849 Share on other sites More sharing options...
JasonLewis Posted August 22, 2008 Author Share Posted August 22, 2008 No that's fine. I'll be doing logs, just trying some things. So you're saying that just using new Exception() is bad? I've never used exceptions and I rarely use sprintf(). Link to comment https://forums.phpfreaks.com/topic/120826-decent-error-reporting/#findComment-622860 Share on other sites More sharing options...
DjMikeS Posted August 22, 2008 Share Posted August 22, 2008 Why not use the try statement ? <?php $query = "SELECT * from TABLE WHERE yadayada = "yadayada"; try { if (!$qResult = mysql_query($query)) { $mysql_error = mysql_error(); throw new Exception("Error bla bla bla <br />Reason: $mysql_error"); return false; } ## Enter the code to run when all goes well here..... } catch (Exception $e) { $error = $e->getMessage(); $script = $e->getFile(); log_error($error, $script); echo "Error bla bla bla."; } ?> And then there's the function to log the error: <?php function Log_error($error, $script) { $date = date("d-m-Y"); $time = date("H-i-s"); $error = mysql_real_escape_string($error); $script = mysql_real_escape_string($script); $insError = ("INSERT INTO error_log (date, time, error, source) VALUES('$date', '$time', '$error', '$script')"); try { if (!$qResult = mysql_query($insError)){ $mysql_error = mysql_error(); throw new Exception("Unable to update error log. <br />Reason: $mysql_error"); } } catch (Exception $e) { echo $e->getMessage(); } } ?> You could also write to a text file or whatever... This will log the error message, script generating the error and the time it occurred... Link to comment https://forums.phpfreaks.com/topic/120826-decent-error-reporting/#findComment-622865 Share on other sites More sharing options...
448191 Posted August 22, 2008 Share Posted August 22, 2008 Check out section 3.8 of my intro into OOP in PHP on the main site for a basic example of using exceptions. Note that you do NOT need to catch an exception that you intend to be fatal! Why not use the try statement ? See my comment above. You could also write to a text file or whatever... This will log the error message, script generating the error and the time it occurred... If you don't want the error to be displayed, toggle the display_errors directive off. And use the error log to log errors. That's what it is for. Logging errors to a database is unwise. Link to comment https://forums.phpfreaks.com/topic/120826-decent-error-reporting/#findComment-622867 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.