Interesting question. Short answer, from my quick tests, the MySQLi connection object
does not need to be in scope for a statement created from it to work, but it
does need to exist. Consider the following sample code:
Code:
<?php
function make_stmt() {
static $ret;
if (! isset($ret))
$ret = new mysqli('localhost', 'user', 'pass', 'test');
$stmt = $ret->prepare('select * from foo where id = ?');
return $stmt;
}
function get_rows($stmt) {
$stmt->bind_param('i', $i=1);
$res = $stmt->execute();
$ret = $stmt->bind_result($i1, $i2, $i3);
if ($ret) while ($stmt->fetch()) {
echo "$i1, $i2, $i3\n";
}
}
$s = make_stmt();
get_rows($s);
As written, this code works just fine, but the MySQLi connection object is clearly out of scope when the statement is executed. However, note that because of the
static declaration, the MySQLi object is not destroyed when the make_stmt function exist. If you remove that, then the object is destroyed when the function exits and not only does the statement not work, but the script segfaults when you try to use it.