Here I want to pass the prepared statements as arguments of test function which should display the query results.But the code below doesn't work,because it may lack something that I don't know.What should I do in order to display results by the test function:
<?php
$mysqli = new mysqli("localhost","root","","test");
/*check connection*/
if(mysqli_connect_errno())
{
printf("connection failed: %s
",mysqli_connect_error());
exit();
}
/*create prapared statement*/
$stmt1 = $mysqli->prepare("select id from posts");
$stmt2 =$mysqli->prepare("select username from members where id=?");
$stmt1->execute();
$stmt1->store_result();
$stmt1->bind_result($ID);
/*bind params*/
$stmt2->bind_param('i',$id);
/*bind results*/
$stmt2->bind_result($username);
while($stmt1->fetch())
{
/*set params*/
$id =$ID;
/*execute prapared statement*/
$stmt2->execute();$stmt2->store_result();
}
test($stmt1,$stmt2);//function call
function test($stmt1,$stmt2)
{
while($stmt1->fetch())
{
while($stmt2->fetch())
{
echo 'Username: '.$username.'<br/>';
}
}
}
?>