SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)

    mysqli try-catch oddities?

    When I put a try-catch around mysqli_connect, specifically passing in bad values, the error never seems to catch. Any clue why this is?

    PHP Code:
    define('_CONN_HOST_''foo');
     
    define('_CONN_USER_''bar');
     
    define('_CONN_PASS_''this');
     
    define('_CONN_DATA_''that');
     
    final class 
    Connection {
     
    function 
    __construct($host _CONN_HOST_$user _CONN_USER_$pass _CONN_PASS_$data _CONN_DATA_) {
     
    try {
     
    mysqli_connect($host$user$pass$data);
     
    } catch (
    Exception $e) {
     
    echo 
    'Error';
     
    }
     
    }
     
    }
     
    $conn = new Connection();
     
    unset(
    $conn); 

  2. #2
    SitePoint Member
    Join Date
    Aug 2004
    Location
    Denmark
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    c/p, php.net/mysqli_connect :
    The mysqli_connect() function attempts to open a connection to the MySQL Server running on host which can be either a host name or an IP address. Passing the NULL value or the string "localhost" to this parameter, the local host is assumed. When possible, pipes will be used instead of the TCP/IP protocol. If successful, the mysqli_connect() will return an object representing the connection to the database, or FALSE on failure.

  3. #3
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use

    PHP Code:
    ...
    if (! 
    $connection mysql_connect'...''...''...' ) ) {
    throw new 
    SqlExceptionmysql_error$connection ) );
    return 
    false;
    }
    mysql_select_db'...'$connection );
    return 
    true;
    ... 
    PHP Code:
    // somewhere in your script you have this
    ...
    try {
    $db = new MySqlConnection;
    } catch( 
    SqlException $e ) {
    die( 
    $e -> getMessage() );
    }
    ... 
    In my view, it is far better to throw an exception and catch it later at a higher layer. In doing so, if at that layer you don't want or need to handle the exception, you throw it again to a (yet) higher layer.

    Okay, it's not for the latest mysql extension but it should still be the same principle no?

  4. #4
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    Livingston, thanks. I also do my real error handling all at the top script level. I just moved to PHP5/mysqli and was testing things, hence the odd code. I'll try yours out later but if I'm not mistaken, and I may be, even if the "$connection = mysql_connect(...)" test fails, the mysqli engine will produce it's own exception. Unless you turn off errors with @. If you do that, you can't catch errors unless you DO test the return. This is totally unlike the way any other function is handled. My question was basically this: is this by design, or a fluke?

  5. #5
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In that event I don't have an answer for you since I've not used the new features available to mysql with this extension

    However I'll try to simulate the effect you have (with @) with the bog standard extension, to see if @ effects the return value

  6. #6
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    Thanks regardless. I think the mysql/mysqli connect functions, and maybe a few others, handle errors internally and simply die with a custoim message instead of throwing exceptions. I tested a few other scenarios and it seems impossible to trap errors from them without resorting specifically to testing return values together with the mysql_error/mysqli_connect_error functions. Oh well...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •