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

发布于 2024-10-18 14:05:15 字数 1054 浏览 3 评论 0原文

里面的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;
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぽ尐不点ル 2024-10-25 14:05:15

PDO 不支持嵌套查询。

您需要首先将整组 ID 读入数组,然后再次循环该数组以生成输出,例如:

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

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

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>";
 }
薄荷→糖丶微凉 2024-10-25 14:05:15

语法错误,您缺少连接点:

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

Syntax error, you are missing a concatination dot:

echo "<div id='avatar'>" . box::avatar($idUser)."</div>"
娇柔作态 2024-10-25 14:05:15

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

Because there are no records in your table matching the condition

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文