MySQLi vs PDO - query execution / functions

2
Looking at questions here from the stack and the php.net site, I saw that the query / function runs in MySQLi uses if() to check whether it was executed or not, and PDO uses try { ... } cacth() { ... } , example connection to bank according to PHP documentation

MySQLi:

<?php
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

mysqli_close($link);
?>

PDO:

<?php
/* Connect to a MySQL database using driver invocation */
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

?>

Why? Do you have any problems using inverted?

    
asked by anonymous 03.03.2018 / 06:25

2 answers

5

Regardless of which option to take, how to enable configuration in My or If is important to know that when invokes Try..Catch means that PHP (in this case) goes into alert mode and if something unexpected will occur, avoiding for example a stack overflow . That is, you should not use this method for everything, an If will always be more performative.

    
03.03.2018 / 17:36
2

For the PDO it's easy and the documentation itself explains:

  

PDO :: __ construct () throws a PDOException if the attempt to connect to the requested database fails.

Since the return of the PDO, in case of an error, is an exception, using try...catch is recommended.

The mysqli_connect documentation is now vague about return. But, quickly testing if it knows that the return is a boolean and if not, the error will be available in mysqli_connect_error . For the return type, a try..catch block is not required, although you can use:

<?php
$link = mysqli_connect('127.0.0.1', 'user', 'pass', 'db');

try {
    if (!$link) {
        throw new RuntimeException(mysqli_connect_error(), mysqli_connect_errno());
    }
} catch (RuntimeException $exception) {
    ...
}

UPDATE Or, as suggested by @Bacco, using mysqli_report and forcing exceptions to be thrown in case of errors:

<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

try {
    $link = mysqli_connect('127.0.0.1', 'user', 'pass', 'db');
} catch (Exception $exception) {
    // caso haja um problema com a conexão, uma exceção será lançada
}    

Since, according to the manual, mysqli_connect is a shortcut to mysqli::__construct , we can observe that return should be an object representing the connection, but not for calls directly via mysqli_connect :

  

Returns an object which represents the connection to a MySQL Server.

In practical terms: use try..catch with the PDO, since without it an error can not be contained and treated and use if or try..catch to mysqli, since both can be used. >

Sources:

03.03.2018 / 07:36