如何获取mysqli_stmt-object的准备好的语句?
如果在执行mysql语句时出错,我想返回该语句。

$id = "89c483c8";
$query = "SELECT * FROM database WHERE id = ?";
if (!($stmt = $database->prepare($query) { ... }
else {
    $stmt->bind_param("s", $id);
    if (!$stmt->execute())
         return $stmt->get_statement; //doesn't exist
}

“$stmt->get_statement”当然不起作用。那么我如何得到完整的查询呢?在本例中:
"SELECT * FROM database WHERE id = 89c483c8"

最佳答案

这是捕获sql错误的最佳方法:

try {
    $res = $mysqli_instance->query($query);
}catch (mysqli_sql_exception $e) {
    print "Error Code <br>".$e->getCode();
    print "Error Message <br>".$e->getMessage();
    print "Strack Trace <br>".nl2br($e->getTraceAsString());
}

或者最简单的方法:
echo $stmt->error

http://php.net/manual/en/mysqli.error.php

关于php - MYSQL错误获取准备好的语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45709421/

10-15 08:32