The mysqli library is a kind of bridge between the procedural and object-oriented programming styles. Therefore, it supports two approaches to error handling: procedural and exceptions.
The first procedural approach - you check the result of each function and if false returned, you get a message using the mysqli_error() function
$db = mysqli_connect("localhost", "user", "...", "test"); if (mysqli_connect_errno()) { echo "ΠΡΠΈΠ±ΠΊΠ° ΡΡΡΠ°Π½ΠΎΠ²ΠΊΠΈ ΡΠΎΠ΅Π΄ΠΈΠ½Π΅Π½ΠΈΡ" . mysqli_connect_error(); exit(); } $sql = "INSERT INTO users (login, email, password, datetime) VALUES (?, ?, ?, ?)"; $stmt = mysqli_prepare($db, $sql); if(!$stmt) { echo "ΠΡΠΈΠ±ΠΊΠ° ΠΏΠΎΠ΄Π³ΠΎΡΠΎΠ²ΠΊΠΈ Π·Π°ΠΏΡΠΎΡΠ°: " . mysqli_error($db); exit(); } if(!mysqli_stmt_bind_param($stmt, 'ssss', $login, $email, $password, $datetime)) { echo "ΠΡΠΈΠ±ΠΊΠ° ΡΠ²ΡΠ·ΡΠ²Π°Π½ΠΈΡ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠΎΠ²: " . mysqli_error($db); exit(); } if(!mysqli_stmt_execute($stmt)) { echo "ΠΡΠΈΠ±ΠΊΠ° Π²ΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ Π·Π°ΠΏΡΠΎΡΠ°: " . mysqli_error($db); exit(); } mysqli_stmt_close($stmt); mysqli_close($db);
Of course, this is not very convenient, especially in object-oriented code, so mysqli allows you to switch to the exception generation mode (there are several types, in debug mode it is most convenient to use MYSQLI_REPORT_ALL ). You can set the mode using the mysqli_report() function mysqli_report()
mysqli_report(MYSQLI_REPORT_ALL); try { $db = mysqli_connect("localhost", "user", "...", "test"); $sql = "INSERT INTO users (login, email, password, datetime) VALUES (?, ?, ?, ?)"; $stmt = mysqli_prepare($db, $sql); mysqli_stmt_bind_param($stmt, 'ssss', $login, $email, $password, $datetime); mysqli_stmt_execute($stmt); mysqli_stmt_close($stmt); mysqli_close($db); } catch (Exception $e) { echo $e->getMessage(); }
Errors are intercepted by the standard exception mechanism. Despite the fact that you have chosen the procedural style, you can use exception catching, which is more often used in object-oriented code.