create table table1 (
col1 int auto_increment primary key
);
create table table2 (
col2 int auto_increment primary key,
col3 int,
col4 int
);
insert into table1 (col1) values (1), (2), (3), (4), (5);
insert into table2 (col2,col3,col4) values (1,1,1), (2,2,1), (3,3,2), (4,4,1), (5,5,1), (6,6,1), (7,7,2), (8,8,1);
<?php
$stmt = $mysqli -> prepare("select col1 from table1");
$stmt -> bind_result($col1);
$stmt -> execute() or die();
$stmt -> store_result();
$stmt2 = $mysqli -> prepare("select col2, col3 from table2 where col4 = ?");
$stmt2 -> bind_param("i", $col1);
$stmt2 -> bind_result($col2, $col3);
while ($stmt -> fetch()) {
$stmt2 -> execute() or die();
$stmt2 -> store_result();
while ($stmt2 -> fetch()) {
echo $col2 .",";
}
$stmt2 -> free_result(); // <== Works fine without this line
echo "---";
}
$stmt -> close();
$stmt2 -> close();
echo "\n\n";
$stmt = $mysqli -> prepare("select col1 from table1");
$stmt -> bind_result($col1);
$stmt -> execute() or die();
$stmt -> store_result();
$stmt2 = $mysqli -> prepare("select col2, col3 from table2 where col4 = ?");
$stmt2 -> bind_param("i", $col1);
$stmt2 -> bind_result($col2, $col3);
while ($stmt -> fetch()) {
$stmt2 -> execute() or die();
$stmt2 -> store_result();
while ($stmt2 -> fetch()) {
echo $col2 .",";
}
//$stmt2 -> free_result(); // <== Works fine without this line
echo "---";
}
$stmt -> close();
$stmt2 -> close();