douti8321 2011-02-22 14:33 采纳率: 0%
浏览 6
已采纳

他在这里工作。 里面不起作用,为什么?

Inside the WHILE the avatar (); not work. And it works outside the WHILE. How do I operate the function within the WHILE?

try{
     $this->conex->beginTransaction();
     $query = $this->conex->prepare("SELECT idUser FROM usuario WHERE id = :id ORDER BY data DESC LIMIT $pagin, $paginaF");
     $query->bindParam(":id", $ID, PDO::PARAM_INT, 20); 
     $query->execute();
     while ($lista = $query->fetch()){
       $idUser = $lista['idUser'];
       echo "<div id='avatar'>"box::avatar($idUser)."</div>";
     }    
//Here he works out of WHILE. Inside it does not work...
echo box::avatar($idUser);
$this->conex->commit();
}catch (PDOException $ex) {
     echo "Erro: " . $ex->getMessage();
}

public function avatar($idUser){
     $idUser = (int) $idUser;
     $query = $this->conex->prepare("SELECT avatar FROM login WHERE id = :id LIMIT 1");
     $query->bindParam(":id", $idUser, PDO::PARAM_INT, 20);
     $query->execute();
     while ($avatar = $query->fetch()){
         $avatar = $avatar['avatar'];
     }
  return $avatar;
}
  • 写回答

3条回答 默认 最新

  • donglin7383 2011-02-22 14:36
    关注

    PDO doesn't support nested queries.

    You need to read the entire set of IDs into an array first, and then loop over that array again to produce your output, e.g.:

     $ids = array();
     $query->execute();
     while ($lista = $query->fetch()){
       $ids[] = $lista['idUser'];
     }
    
     foreach ($ids as $idUser) {
       echo "<div id='avatar'>" . box::avatar($idUser) . "</div>";
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于单片机数字电压表电路组成及框图
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line