Question

I am a little confused as to how prepared statements of mysqli are handled internally.

mysqli::prepare($query); returns a mysqli_stmt object. The documentation says that statements should be closed immediately after they are no longer needed. So far, so good.

However, do statements still need to be closed explicitly if I call mysqli::close()? Also, do the statement objects have their own database connection and will continue to work reliably even in a situation like this:

<?php
$oDb = new mysqli('localhost', 'root', '', 'test');
$oStmt = $oDb->prepare("INSERT INTO tbl_test (a,b,c) VALUES (?,?,?)");

$oDb->close();

$oStmt->bind_param('sss', 'Hello', 'World', '!');
$oStmt->execute();
//do some more stuff
$oStmt->close();

Or will calling mysqli::close() also close all statements?

Was it helpful?

Solution

Yes, calling mysqli::close() also close all statements.
If you are calling this function as the last operator in your code, you don't need to call it either.

So, just don't bother.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top