So i have simple code:
try{
$mysqli = new mysqli($sql_login['host'], $sql_login['user'], $sql_login['password'] , $sql_login['database'], $sql_login['port'] );
if($mysqli->connect_errno){
throw new Exception("Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error);
}
}
catch (Exception $e) {
echo 'Exception: ', $e->getMessage(), "\n";
}
the problem is that php returns error and also the exception. Is there something like in java with throw and throws?
Put an @ sign in front of the call to connect. That will suppress the error message.
You can use PHP's
set_error_handler()
.You can start with installing your own error-handling. One that converts PHP errors into exception. Do it at the beginning of your script. Something likes this:
Now you can use exceptions as the main error-handling mechanism. All you need now, is to catch the exceptions at the right location in your script and display errors yourself.
You can extend this mechanism to include the assert-handling also:
And just accept PHP is not Java or C++. It is a inconsistent mess.