MYSQL错误获取准备语句

时间:2017-08-16 09:07:33

标签: php mysql prepared-statement

如何获取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"

1 个答案:

答案 0 :(得分:1)

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

const enhancer = compose(
  applyMiddleware(ReduxThunk),
  applyMiddleware(logger),
  persistState(null, 'redux')
);

或者最简单的方法:

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());
}

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