I use the preparation method as follows:
$db= new mysqli("localhost","***","***","***"); if ($db->connect_error) { die('Connection Error'); } $id = 1; if($stmt = $db->prepare('SELECT name FROM table WHERE id = ? ')) { $stmt->bind_param('i', $id); $stmt->execute(); // $stmt->close(); echo "Success<br>"; } else { echo "Something broke :/<br>"; } $id =2; if($stmt = $db->prepare('SELECT name FROM table WHERE id = ? ')) { $stmt->bind_param('i', $id); $stmt->execute(); echo "Success<br>"; } else { echo "Something broke :/<br>"; $error = $db->errno . ' ' . $db->error; echo $error; }
If I execute the script, I get
Success
Something broke:/
0
How can I find out why the preparation method failed after it was called a second time? For some reason, $db->errno returns 0, which indicates that nothing happened. However, the preparation methods fail and return false, so I cannot check $stmt->error;
I accidentally found out that when I delete the first call to $stmt->execute() , the preparation method call works fine again (on the first and second calls). What am I missing here?
Edit
As Maxim Tkach suggested, if I uncomment
// $stmt->close();
then i get
Success
Success
But why is that? I have never read anywhere that it is very important to close an expression about preparation.
This is from the PHP manual:
Closes a prepared statement. mysqli_stmt_close () also frees the statement. If the current statement is awaiting or not reading the results, this function cancels them, so the next request can be executed.
I donβt see what they say that it is important to close it in order to fulfill the second expression about preparation. Did I miss something?
php
Adam May 11 '16 at 1:36 pm 2016-05-11 13:36
source share