1
Looking at questions here from the stack and the php.net website, I saw that query/function executions in Mysqli use if()
to check whether it was executed or not, and the PDO uses try { ... } cacth() { ... }
, example of connection to database 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 problem using inverted?
On mysqli you can choose to use exception or not natively. On PDO it is the normal mechanism. To throw a random exception (as in a DB error) I understand as a failure to understand the mechanism, in favor of programming "fashions" (such "good practices", which are only good for real in the academic world). Exception in PHP is usually something worthy of Rube Goldberg
– Bacco
For use with mysqli: https://secure.php.net/manual/en/mysqli-driver.report-mode.php in particular the flag
MYSQLI_REPORT_STRICT
that makes mysqli use exceptions– Bacco
@Bacco can make a response by exemplifying how to treat possible errors (
Exception
)? With a list of errors and explaining what each one is– Costamilam