简体   繁体   中英

Prepared statement inside a prepared statement

I've been trying to get the id of each person rostered to a certain event. Which works. However this for some reason doesn't work. When it did before. I realised while testing that when i put the $userRes->close right after the while($userRes->fetch()) the $user = getUser($connection, $id) works, but when it is outside the loop it doesn't work. Here is my code:

$userQuery = "SELECT person_id FROM RosterPeopleEvent WHERE news_id=?;";
$userRes = $connection->prepare($userQuery);
$userRes->bind_param('i', $eventID);
$userRes->execute();
$userRes->bind_result($id);

while ($userRes->fetch()){
    $user = getUser($connection, $id);
    if ($user == $_SESSION["uid"]){
        $p->addContent("You have volunteered for the BBQ <a href=roster.php?rosterid=1&personid=$id&eventid=$eventID&class=remove rel=external>Remove</a>, ");  
    }
    else{
        $p->addContent("$user has volunteered for the BBQ <a href=roster.php?rosterid=1&personid=$id&eventid=$eventID&class=remove rel=external>Remove</a>, "); 
    }                           
}
$userRes->close();      

Fixed version:

$userQuery = "SELECT person_id FROM RosterPeopleEvent WHERE news_id=?;";
$userRes = $connection->prepare($userQuery);
$userRes->bind_param('i', $eventID);
$userRes->execute();
$userRes->bind_result($id);
$userRes->store_result();

while ($userRes->fetch()){
    $user = getUser($connection, $id);
    if ($user == $_SESSION["uid"]){
        $p->addContent("You have volunteered for the BBQ <a href=roster.php?rosterid=1&personid=$id&eventid=$eventID&class=remove rel=external>Remove</a>, ");  
    }
    else{
        $p->addContent("$user has volunteered for the BBQ <a href=roster.php?rosterid=1&personid=$id&eventid=$eventID&class=remove rel=external>Remove</a>, "); 
    }                           
}
$userRes->close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM