1

How do I get the prepared statement of the mysqli_stmt-object?

If there is an error while executing the mysql-statement I want to return the statement.

$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" of course doesn't work. So how do I get the full query? In this example:

"SELECT * FROM database WHERE id = 89c483c8"
GURKE
  • 134
  • 2
  • 11

1 Answers1

2

This is the best way to catch sql errors :

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

Or the simplest way :

echo $stmt->error

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

Malek Zarkouna
  • 943
  • 10
  • 21