1

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?

user1564141
  • 5,911
  • 5
  • 21
  • 18
  • Possible dublicate http://stackoverflow.com/questions/4862911/how-to-throw-an-error-in-mysql-procedure – Peon Aug 06 '12 at 10:39
  • `ini_set('display_errors', 0);` - for better or worse (I'm in the worse camp) errors and exceptions are two completely different things in PHP. – DaveRandom Aug 06 '12 at 10:40
  • @DainisAbols it isnt mysql error. – user1564141 Aug 06 '12 at 10:41
  • @DaveRandom i can also use @$mysqli..., but it is not the solution i want. – user1564141 Aug 06 '12 at 10:41
  • @user1564141 Well unfortunately you have no choice. If something triggers an error, and your error reporting settings are such that it will be displayed, your only option is to suppress it. Like I said, I'm very much in the "worse" camp on this point but you work with what you've got... – DaveRandom Aug 06 '12 at 10:43
  • @DaveRandom thanks a lot... for no i try @, but i think there is some solution on this... – user1564141 Aug 06 '12 at 10:46
  • You can convert errors into exceptions, always, with the help of an error handler, please see [`ErrorException`](http://de.php.net/manual/class.errorexception.php) in the manual. – hakre Aug 06 '12 at 10:53
  • Also: don't forget to close your connection. In the above script your connection stays open beyond its scope. PHP does no clean-up for you like Java or C++. – Patrick Savalle Aug 06 '12 at 12:09

3 Answers3

2

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:

/*
|--------------------------------------------------------------------------
| Alternative error handler
|--------------------------------------------------------------------------
|
| See: http://php.net/manual/en/function.set-error-handler.php
|
*/
function my_error_handler($errno, $errstr, $errfile, $errline)
{
    if (!(error_reporting() & $errno))
    {
        // This error code is not included in error_reporting
        return;
    }
    throw new ErrorException( $errstr, $errno, 0, $errfile, $errline );
}
ini_set('display_errors', FALSE);
set_error_handler("my_error_handler");

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:

/*
|--------------------------------------------------------------------------
| Assert handling
|--------------------------------------------------------------------------
|
|   See: http://php.net/manual/en/function.assert.php 
|
*/
function my_assert_handler($file, $line, $code)
{
    throw new Exception( "assertion failed @$file::$line($code)" );
}
assert_options(ASSERT_ACTIVE,     1);
assert_options(ASSERT_WARNING,    0);
assert_options(ASSERT_BAIL,       0);
assert_options(ASSERT_QUIET_EVAL, 0);
assert_options(ASSERT_CALLBACK, 'my_assert_handler');

And just accept PHP is not Java or C++. It is a inconsistent mess.

Patrick Savalle
  • 4,068
  • 3
  • 22
  • 24
  • the shit happens, when appear error in sql sintax - it dosend work with it and as a result all script crashes with fatal error. – user1564141 Aug 06 '12 at 13:01
  • fatal error's can't be caught in PHP. The script just terminates. Probably some final handler is called, but all context is gone. – Patrick Savalle Aug 06 '12 at 13:17
0

You can use PHP's set_error_handler().

Shubham
  • 21,300
  • 18
  • 66
  • 89
0

Put an @ sign in front of the call to connect. That will suppress the error message.

iWantSimpleLife
  • 1,944
  • 14
  • 22