14

I'm following an OOP mysqli course. When connecting to the database, they use the following script:

$db = new mysqli("host", "user", "password", "database");
if ($db->connect_error){
    $error = $db->connect_error;
    echo("Not connected: " . $error);
}

Later though, they call the database connection file with a try / catch block:

try {
    require_once "connection.php";
}catch (Exception $e){
    $error = $e->getMessage();
    echo $error;
}

Isn't a possible connection error being handled by the connection file immediately after trying to connect? Is the try / catch block basically doing the same thing? Or is the try / catch block looking for a different type of error?

UPDATE: Just to clarify, after reading some of the answers. When I just do this:

try {
    $db = new mysqli("host", "user", "password", "database");
}catch (Exception $e){
    $error = $e->getMessage();
    echo $error;
}

assuming that the database access data is wrong (for example a wrong host), I get a PHP warning but not the error output in the catch block. Shouldn't the catch detect this error?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
cesarcarlos
  • 1,271
  • 1
  • 13
  • 33
  • 1
    Please add a link to the course – Jakub Filipczyk Dec 07 '14 at 17:21
  • If the connection file was throwing an exception, and if the catch block terminated the script after displaying any exception, I'd be inclined to agree.... but in reality nothing is actually handling any connection problem cleanly, because the script will continue executing regardless of whether it has a database connection or not – Mark Baker Dec 07 '14 at 17:26
  • joebezucha, the course is called Accessing Databases with Object-Oriented PHP from lynda.com – cesarcarlos Dec 07 '14 at 17:54
  • 1
    http://www.lynda.com/PHP-tutorials/Accessing-Databases-Object-Oriented-PHP/169106-2.html – cesarcarlos Dec 07 '14 at 18:18

3 Answers3

20

If you need to catch exceptions on mysqli extension by use try/catch block, try this code (switch on exception mode instead of classic error reporting):

// Method 1:
$driver = new mysqli_driver();
$driver->report_mode = MYSQLI_REPORT_STRICT | MYSQLI_REPORT_ERROR;

// OR Method 2:
mysqli_report(MYSQLI_REPORT_STRICT | MYSQLI_REPORT_ALL);

try {
    $db = new mysqli("host", "user", "password", "database");
} catch (mysqli_sql_exception $e) {
    ...
}

mysqli_sql_exception mysqli_driver

2

In the first section of code when you use the if statement, you are checking to see if that one condition is true and then outputting your message.

A try catch block essentially works like this

try{
   //place code here that could potentially throw an exception
}
catch(Exception $e)
{
  //We will catch ANY exception that the try block will throw

}

So you see that while your if statement is checking for a condition that you are anticipating, the try catch block will detect anything that goes wrong, even those things that you don't anticipate. Therefore, when debugging you can alter the code in the catch block to deal with exceptions as you see fit

See the PHP docs for more information about exceptions http://php.net/manual/en/language.exceptions.php

KyleHodgetts
  • 317
  • 1
  • 4
  • 13
  • Thanks, Kyle. I assumed that the only error that could take place was the connection error checked by $db->connect_error. Just wondering what other type of exceptions could be detected by the try/catch when trying to connect to a database. – cesarcarlos Dec 07 '14 at 17:31
  • When you use a catch block with Exception $e, this will catch any exception that is thrown, even if it is just the connection exception. I found this question that also may be of some use to you. http://stackoverflow.com/questions/9836693/how-to-use-throw-exception-in-mysql-database-connect – KyleHodgetts Dec 07 '14 at 17:34
  • That's where I get confused. If I enter wrong parameters when trying to connect to the database (please see the update I added to my post) the catch block is not executed (no error is echoed). Is the connect error not treated as an exception? Thanks – cesarcarlos Dec 07 '14 at 18:00
  • 1
    If you see the link I posted to the other question regarding this type of exception, he combined the two methods by checking if a condition is true and if it is, throwing a new exception to be caught by the catch block. Try that – KyleHodgetts Dec 07 '14 at 18:04
-2

Are you asking about what's the different with $db->connect_error and try/catch?

$db->connect_error mostly for the errors which we already knew (username incorrect, etc.)

and try/catch is about system level, once an error occur,

PHP will search for the nearest try/catch block,

once it catch something, PHP will still continue like nothing happened before,

but if you don't have any try/catch block and set_exception_handler() too,

PHP will just stop by the error.