在准备好的声明中准备好的声明

时间:2013-11-12 20:53:13

标签: php loops prepared-statement

我一直在努力让每个人的身份都出现在特定事件中。哪个有效。 然而,由于某种原因这不起作用。什么时候呢。我在测试的时候意识到,当我把$ userRes->关闭在while($ userRes-> fetch())之后,$ user = getUser($ connection,$ id)工作,但是当它在循环之外时它不起作用。这是我的代码:

$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();      

1 个答案:

答案 0 :(得分:0)

修正版:

$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();