thepip3r Posted January 19, 2009 Share Posted January 19, 2009 My question is, what are the differences (if any) of: $q = mysql_query("SELECT COUNT(*) FROM quotes"); if (!$q) { errorHandler("mysql", 1); } vs. $q = mysql_query("SELECT COUNT(*) FROM quotes") or die(errorHandler("mysql", 1)); Any clarification on best practices, etc would be greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/141499-solved-if-val-vs-or-die-difference-benefits-any-guidance/ Share on other sites More sharing options...
flyhoney Posted January 19, 2009 Share Posted January 19, 2009 It depends, what does errorHandler("mysql", 1) do? Link to comment https://forums.phpfreaks.com/topic/141499-solved-if-val-vs-or-die-difference-benefits-any-guidance/#findComment-740645 Share on other sites More sharing options...
trq Posted January 19, 2009 Share Posted January 19, 2009 Bringing down an entire script (calling die) because of an error is often overkill. If you can catch an error yourself and handle it more gracefully its better. Link to comment https://forums.phpfreaks.com/topic/141499-solved-if-val-vs-or-die-difference-benefits-any-guidance/#findComment-740654 Share on other sites More sharing options...
flyhoney Posted January 19, 2009 Share Posted January 19, 2009 Yeah, the abrupt die() statements you see here on phpfreaks and elsewhere are really for debugging purposes. When you are dealing with live applications, like Thorpe said, it better to deal with errors gracefully. At the very least, displaying a full error page to the user as opposed to just exiting the script. Link to comment https://forums.phpfreaks.com/topic/141499-solved-if-val-vs-or-die-difference-benefits-any-guidance/#findComment-740670 Share on other sites More sharing options...
flyhoney Posted January 19, 2009 Share Posted January 19, 2009 If you want to get fancy you should look into exception handling: set_exception_handler('exception_handler'); $q = mysql_query("SELECT COUNT(*) FROM quotes"); if (!$q) { throw new Database_Exception('blah blah blah'); } class Database_Exception extends Exception { protected $message = 'Database Exception'; } function exception_handler(Exception $e) { //use this function to display a full error page echo $e->getMessage(); } Link to comment https://forums.phpfreaks.com/topic/141499-solved-if-val-vs-or-die-difference-benefits-any-guidance/#findComment-740676 Share on other sites More sharing options...
thepip3r Posted January 19, 2009 Author Share Posted January 19, 2009 so... there's an implicit exit(); in die(); then? i think that's what you guys are getting at and I do see the added value of letting the rest of your page load and just throwing a custom error. thank you for the clarification. @flyhoney - errorHandler() is my custom function that formats error messages certain ways, exits, and displays certain errors or not depending on whether i'm debugging or not. currently, i'm using the if (!$val) structure so I didn't realize the implicit exit(); within die(); thanx again gents. Link to comment https://forums.phpfreaks.com/topic/141499-solved-if-val-vs-or-die-difference-benefits-any-guidance/#findComment-740685 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.