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

时间:2011-02-22 14:33:21

标签: php pdo

在WHILE中的头像();不行。它在WHILE之外工作。如何在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 个答案:

答案 0 :(得分:4)

语法错误,你缺少一个连接点:

echo "<div id='avatar'>" . box::avatar($idUser)."</div>"

答案 1 :(得分:4)

PDO doesn't support嵌套查询。

您需要首先将整个ID读取到数组中,然后然后再次遍历该数组以产生输出,例如:

 $ids = array();
 $query->execute();
 while ($lista = $query->fetch()){
   $ids[] = $lista['idUser'];
 }

 foreach ($ids as $idUser) {
   echo "<div id='avatar'>" . box::avatar($idUser) . "</div>";
 }

答案 2 :(得分:0)

因为您的表中没有符合条件的记录