About prepared statement, theoricaly, you will know your kwery is bad formatted before sending the parameters. So you will not have to wonder if it is your query or the user datas which are bugged, you know it immediately.
About general queries, as mysqli is in sync with MySQL5 whereas mysql_ old API supports only until MYSQL 4, you will have better error message, or at least most descriptive.
Moreover you can use the try\catch syntax to detect mysql errors and it is easier to deal with exceptions instead of or die....
or if(mysql_error())
.
from documentation you have :
<?php
define("MYSQL_CONN_ERROR", "Unable to connect to database.");
// Ensure reporting is setup correctly
mysqli_report(MYSQLI_REPORT_STRICT);
// Connect function for database access
function connect($usr,$pw,$db,$host) {
try {
$mysqli = new mysqli($host,$usr,$pw,$db);
$connected = true;
} catch (mysqli_sql_exception $e) {
throw $e;
}
}
try {
connect('username','password','database','host');
echo 'Connected to database';
} catch (Exception $e) {
echo $e->errorMessage();
}
the key function is mysqli_report(MYSQLI_REPORT_STRICT)
, according to documentation :
MYSQLI_REPORT_STRICT
Throw a mysqli_sql_exception for errors instead of warnings.
there is also a mysqli_debug
function/method as eis said.