how to use throw exception in mysql database connect

30,922

Solution 1

try
{
    if ($db = mysqli_connect($hostname_db, $username_db, $password_db))
    {
        //do something
    }
    else
    {
        throw new Exception('Unable to connect');
    }
}
catch(Exception $e)
{
    echo $e->getMessage();
}

Solution 2

It is documented here http://ie2.php.net/manual/en/mysqli.error.php

if (mysqli_connect_errno()) {
    throw new RuntimeException("Connect failed: %s\n", mysqli_connect_error());
}
Share:
30,922
Django Anonymous
Author by

Django Anonymous

Updated on March 23, 2020

Comments

  • Django Anonymous
    Django Anonymous about 4 years

    I come across this:-

    PHP Error handling: die() Vs trigger_error() Vs throw Exception

    and understood that throw exception is better

    How can i replace die and use throw exception here in this code:-

    <?php
    # FileName="Connection_php_mysql.htm"
    # Type="MYSQL"
    # HTTP="true"
    $hostname_db = "localhost";
    $database_db = "database";
    $username_db = "root";
    $password_db = "password";
    $db = mysqli_connect($hostname_db, $username_db, $password_db) or die("Unable to connect with Database"); 
    ?>
    
  • Django Anonymous
    Django Anonymous about 12 years
    i need to replace die("Unable to connect with Database"); with your code? Also can you tell me what is @$_POST['variable']
  • slash197
    slash197 about 12 years
    You should replace your last line with this. @ - is enabling silent mode, no error will be echoed but still logged in your server's error.log file. I guess you know what $_POST['variable'] is.
  • Django Anonymous
    Django Anonymous about 12 years
    yes i do know that... thanks for the info :)
  • Django Anonymous
    Django Anonymous about 12 years
    ok $db = mysqli_connect($hostname_db, $username_db, $password_db) or die("Unable to connect with Database"); I have to replace with this $db = mysqli_connect($hostname_db, $username_db, $password_db); if (mysqli_connect_errno()) { throw new RuntimeException("Connect failed: %s\n", mysqli_connect_error()); }
  • Django Anonymous
    Django Anonymous about 12 years
    one more thing, as i have added the code in an include_db file for database connection... so what i should put in the area //do something because it just need to do connect or show the error...
  • slash197
    slash197 about 12 years
    You can leave that empty, if there's no error the execution will continue with code what is after the try...catch block.
  • Django Anonymous
    Django Anonymous about 12 years
    see this file is my database connection file, which i include on every page of my website.... these codes are just to connect with the database other things i do on their respective pages.
  • slash197
    slash197 about 12 years
    That shouldn't be a problem, as I've said if there's no error PHP will continue as if nothing happened.
  • Django Anonymous
    Django Anonymous about 12 years
    can i change this like this to get things done... try { if (!$db = mysqli_connect($hostname_db, $username_db, $password_db)) { throw new Exception('Unable to connect'); } else { $db = mysqli_connect($hostname_db, $username_db, $password_db); } } catch(Exception $e) { echo $e; }
  • slash197
    slash197 about 12 years
    Test it, I don't know if it would work. Which I'm sure of is that if there is no error the mysqli_connect function will be executed twice.