三选 - 查询

时间:2014-03-01 21:15:15

标签: php select nested

我正在研究评论的输出功能。 但现在我遇到了一个大问题:我必须从评论,用户名和用户头像中选择数据。他们每个人都在另一张桌子里。我真的不喜欢“加入”,所以我尝试了嵌套查询。没有成功,我希望有人可以帮助我一点。 这是我的代码:

<?php    
     function showComments($con, $a)
     {
        if($comments = $con->prepare("SELECT * FROM anime_comment WHERE ani_id = $a"))
        {
            $comments->execute();
            $comments->bind_result($id, $animeId, $orating, $srating, $drating, $mrating, $crating, $comment, $cdate, $userId);

            while($comments->fetch())
            {
                if($username = $con->prepare("SELECT username FROM a1_users WHERE id = $userId"))
                {
                    $username->execute();
                    $username->bind_result($username);
                    $username->fetch();
                    $username->close();
                }

                if($avatar = $con->prepare("SELECT avatar FROM a1_comprofiler WHERE user_id = $userId"))
                {
                    $avatar->execute();
                    $avatar->bind_result($userAvatar);
                    $avatar->fetch();
                    $avatar->close();
                }

                        echo '<table class="comment" align="center" style="width:1000px">';
                        echo '<tr>';
                        echo '<td>'.$userAvatar.'<td>';
                        echo '<td>'.$comment.'</td>';
                        echo '<td>'.$username.'</td>';
                        echo '</tr>';           
                        echo '</table>';                    
            }
        }
     }
?>

0 个答案:

没有答案