Hi,
I am trying to slowly convert some old MySQL based code to the newer MySQLi
extension and have come across something that I want to call a bug, but thought
maybe as a newb with the extension that this might be something at least a few
of you are familiar with. Basically depending on where I call a function from
I either get errors but there does not seem to be much rhyme or reason too it.
Here is a very simple code sample -- yes its procedural ...
<?php
$dbuser = "auser";
$dbpass = "apass";
$dbname = "mydb";
$dbhost = "localhost";
$dbport = 3306;
$db = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname, $dbport);
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') ' .
mysqli_connect_error());
}
function sub($db) {
$authkey = '';
$sql = "select kv from obj order by kv limit 5";
$stmt = mysqli_prepare($db, $sql);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $authkv);
while (mysqli_stmt_fetch($stmt)) {
echo "<p> SUB: $authkv</p>";
}
mysqli_stmt_close($stmt);
}
function main($db) {
$authkey = '';
$sql = "select kv from obj order by kv limit 5";
$stmt = mysqli_prepare($db, $sql);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $authkv);
while (mysqli_stmt_fetch($stmt)) {
echo "<p>MAIN: $authkv</p>";
}
sub($db);
mysqli_stmt_close($stmt);
}
main($db);
?>
I get the output I expect:
MAIN: 7
MAIN: 8
MAIN: 9
MAIN: 10
MAIN: 11
SUB: 7
SUB: 8
SUB: 9
SUB: 10
SUB: 11
This works perfectly however when I call sub from within the while loop in the
function main this break down. Here is the modification to main:
function main($db) {
$authkey = '';
$sql = "select kv from obj order by kv limit 5";
$stmt = mysqli_prepare($db, $sql);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $authkv);
while (mysqli_stmt_fetch($stmt)) {
echo "<p>MAIN: $authkv</p>";
sub($db);
}
mysqli_stmt_close($stmt);
}
This simple change results in the output.
MAIN: 7
Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, boolean
given in /var/www/html/xgwebapi/mysqli.php on line 21 Warning:
mysqli_stmt_bind_result() expects parameter 1 to be mysqli_stmt, boolean given
in /var/www/html/xgwebapi/mysqli.php on line 22 Warning: mysqli_stmt_fetch()
expects parameter 1 to be mysqli_stmt, boolean given in
/var/www/html/xgwebapi/mysqli.php on line 24 Warning: mysqli_stmt_close()
expects parameter 1 to be mysqli_stmt, boolean given in
/var/www/html/xgwebapi/mysqli.php on line 28
MAIN: 8
Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, boolean
given in /var/www/html/xgwebapi/mysqli.php on line 21 Warning:
mysqli_stmt_bind_result() expects parameter 1 to be mysqli_stmt, boolean given
in /var/www/html/xgwebapi/mysqli.php on line 22 Warning: mysqli_stmt_fetch()
expects parameter 1 to be mysqli_stmt, boolean given in
/var/www/html/xgwebapi/mysqli.php on line 24 Warning: mysqli_stmt_close()
expects parameter 1 to be mysqli_stmt, boolean given in
/var/www/html/xgwebapi/mysqli.php on line 28
I do not understand why this would be the case -- clearly. Does anybody have a
clue for me? I am using PHP 5.3.3 on CentOS. And I am very very very stuck.
James