|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31745 Exception doens't catch connection error
Submitted: 2005-01-28 16:27 UTC Modified: 2005-01-28 21:24 UTC
From: tril2632 at hotmail dot com Assigned:
Status: Not a bug Package: MySQLi related
PHP Version: 5.0.3 OS: Linux Debian
Private report: No CVE-ID: None
 [2005-01-28 16:27 UTC] tril2632 at hotmail dot com

The problem is that exception are not catching connection problems when instanciate the mysqli class.

Reproduce code:

/* Create custom exception classes */ 
class ConnectException extends Exception {} 
class QueryException extends Exception {} 

class example_mysqli extends mysqli 
  function __construct($adr, $login, $mdp, $nom_bd) 
  /* Pass all arguments passed to the constructor on to the parent's constructor */ 
    $args = func_get_args(); 
    // OK if(!@parent::__construct($adr, $login, $mdp, $nom_bd))
    if(!parent::__construct($adr, $login, $mdp, $nom_bd))
      throw new ConnectException(mysqli_connect_error(), mysqli_connect_errno());    	

   /* Throw an error if the connection fails */ 
      throw new ConnectException(mysqli_connect_error(), mysqli_connect_errno()); 
   catch (Exception $e)
   	throw new ConnectException(mysqli_connect_error(), mysqli_connect_errno()); 

  function query($query) 
    $result = parent::query($query); 
      throw new QueryException(mysqli_error($this), mysqli_errno($this)); 
    return $result; 

try { 
  $my = new example_mysqli('loclhost','root', '', 'test'); 
  $result = $my->query("SELCT NOW()"); 

catch(Exception $exception) { 
  echo "Connection Error\n"; 
catch(QueryException $exception) { 
  echo "Query Error\n"; 

/* Handle exceptions that we weren't expecting */ 
catch(Exception $exception) { 
  echo "Who was that masked exception?\n"; 




Expected result:
it should print me "connection Error" 

Actual result:
it always return this error : 

 mysqli::mysqli() [function.mysqli]: Unknown MySQL server host 'loclhost' (11001) in c:\wamp\www\wmi\mysqli.php on line 16

Connection Error

With exception php error should NOT be display only the message in the "catch" should appear !


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2005-01-28 21:24 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

Using exceptions doesn't mean that all warnings/notices/etc. are supressed.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 19 22:01:32 2024 UTC