Here's the code I'm faced with currently...
Code: Select all
// SQL CONNECTION ESTABLISHED, SIMPLE WRAPPER FUNCTION
db_connect($sql_cfg_array);
$sql_query = <<<SQL
SELECT id FROM whatever WHERE thing=something
SQL;
$sql_result = mysql_query($sql_query);
$sql_result_count = mysql_num_rows($sql_result);
$sql_error_msg = mysql_error();
$sql_error_code = mysql_errno();
// SQL ERROR CHECK
if ($sql_error_code !== 0) {
$error_msg[] = 'DB Error: '.$sql_error_code.' '.$sql_error_msg;
} elseif ($sql_result_count < 1) {
// NO RESULTS
$error_msg[] = 'No results were found';
} else {
// LOOP THROUGH DATA
while ($sql_data = mysql_fetch_assoc($sql_result)) {
// DO STUFF
}
}
// ALL SORTS OF OTHER CODE, MORE QUERIES, ANYTHING
// FINAL ERROR CHECK BEFORE OUTPUT
if ( (is_array($error_msg) and count($error_msg) > 0) or (strlen($error_msg) > 0)) {
$error_output = error_process($error_msg);
}
// OUTPUT PAGE USING TEMPLATE
page_output($headers, $theme, $template, $placeholders);An array is set containing any error messages. This array is checked to see if there's been an error. If there has been an error then an error output variable is set by a custom function error_process() which loops through the array and converts it to an HTML ordered list of errors. This is then output in the HTML body code. The error_process() function is not registered as PHP's error handler so this is a different layer of error handling, application errors, so they can be displayed easier. It's all very old and procedural.
With PDO it seems the recommended thing is to use try { } catch { }
Code: Select all
try {
$conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}Or should I leave this as errors but use PHP's error handling with a custom handler?
The problem I have is that no results doesn't always have to be a fatal error that stops the script execution. With most errors I want script execution to actually continue so it can get to the page output part at the bottom of the script so it can output the errors using my HTML template, and give users a few navigation options.
Because this is all procedural, the script checks for errors as it goes along, nesting ifs when there's something that should only be executed when there's been no errors. Then it deals with the actual display (and internal logging) of these errors all at the bottom of each script.
With PHP's built-in error handling and writing a custom error handler, how can I check if there's been an error as I go along?
If I use trigger_error() I assume that calls the error handler straight away and stops execution after the handler?
I completely get the MVC structure but I don't want to use a framework for this project just yet, the directory structure required by most frameworks just won't work. I do already have a crude implementation but it's more C+M with separate V using an HTML template. Scripts are called directly by name /whatever.php.
Even if I was using a framework, I would still need to come up with a structure of checking for and branching on application-level errors and it's that that I'm trying to solve here really.
After some googling I've read that I should still handle exceptions and errors separately, not just completely switch to exceptions everywhere.
In general what's the best way to structure/branch scripts for best error/exception handling?
Even converting this whole thing to classes instead of functions, I think I would still need to be setting some sort of error flag variable if I want to keep branching when there's been "non-fatal" application errors.
Or do I need to treat all application errors the same, and use a custom error handler function if I want to output the errors within my template (view)? That way I suppose I know my script execution halts on any errors so I need less branching and won't end up with 3 layers of nested ifs in some cases - as the only way a particular bit of code will execute is if no error has been triggered.
Cheers, B