在这里,他不在WHILE工作.里面不起作用,为什么? [英] Here he works out of WHILE. Inside it does not work, Why?

查看:59
本文介绍了在这里,他不在WHILE工作.里面不起作用,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WHILE头像内();不行.它可以在WHILE之外运行.如何在WHILE中操作该功能?

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;
}

推荐答案

PDO 不支持嵌套查询.

您需要先将整个ID集读入一个数组,然后然后再次遍历该数组以产生输出,例如:

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>";
 }

这篇关于在这里,他不在WHILE工作.里面不起作用,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆