PDA

Click to See Complete Forum and Search --> : [RESOLVED] MySQL Error Trapping


TheBigB
Jun 13th, 2009, 03:16 PM
Hi there,

The last two hours I've been fiddling around trying to build a script for MySQL error trapping.
The problem particularly is mysql_select_db where I can't catch the error.

What I have at the moment:
function db_select($dbame){
$res = @mysql_select_db($dbname, $conn);
if (mysql_errno() > 0) {
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'error.php?code=6002&from='.$_SERVER['PHP_SELF'];
header("Location: http://$host$uri/$extra");
}
}
It doesn't give an error if a incorrect table name is used.
The question is:
Am I expecting php to give an error while it shouldn't or is there something wrong with the code?

By the way, if someone has a better redirection script, I would appreciate it.

SambaNeko
Jun 13th, 2009, 04:34 PM
Your problem is the "@" in front of @mysql_select_db - this is a special character that suppresses errors from functions! :) Take it out and you should get an error as expected.

TheBigB
Jun 14th, 2009, 05:36 AM
Your problem is the "@" in front of @mysql_select_db - this is a special character that suppresses errors from functions! :) Take it out and you should get an error as expected.
No, the '@' is to stop php from generating an error message, so you can make your own. It still should fire an error internally.

TheBigB
Jun 14th, 2009, 05:53 AM
Found it... Huge typo in the function parameter
function db_select($dbname){

Working function:
function db_select($dbname){
$res = @mysql_select_db($dbname);
if ($res == false) {
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'error.php?code=6002&from='.$_SERVER['PHP_SELF'];
header("Location: http://$host$uri/$extra");
}
}

SambaNeko
Jun 14th, 2009, 09:04 AM
No, the '@' is to stop php from generating an error message, so you can make your own. It still should fire an error internally.

Yes, right, or else why would you be writing your own error handling? Duh.

Sorry for my big fat dumb.