11

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"); 
?>
Community
  • 1
  • 1
Django Anonymous
  • 2,987
  • 16
  • 58
  • 106

2 Answers2

16
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();
}
slash197
  • 9,028
  • 6
  • 41
  • 70
  • i need to replace `die("Unable to connect with Database");` with your code? Also can you tell me what is `@$_POST['variable']` – Django Anonymous Mar 23 '12 at 09:31
  • 2
    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. – slash197 Mar 23 '12 at 09:34
  • yes i do know that... thanks for the info :) – Django Anonymous Mar 23 '12 at 09:35
  • 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... – Django Anonymous Mar 23 '12 at 09:45
  • You can leave that empty, if there's no error the execution will continue with code what is after the `try...catch` block. – slash197 Mar 23 '12 at 09:52
  • 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. – Django Anonymous Mar 23 '12 at 09:53
  • That shouldn't be a problem, as I've said if there's no error PHP will continue as if nothing happened. – slash197 Mar 23 '12 at 09:55
  • 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; } – Django Anonymous Mar 23 '12 at 09:56
  • 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. – slash197 Mar 23 '12 at 09:58
3

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());
}
floriank
  • 25,546
  • 9
  • 42
  • 66
  • 1
    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 Mar 23 '12 at 09:40