mysqli_num_rows生成无限量的数据

时间:2019-03-16 19:55:27

标签: php mysqli

因此,我将尽我所能用英语来尽力解释它。 我基本上是想从数据库中计算数据-我有多少个具有相同ID的字段。这是数据库中的图片。

image from database

例如,在帖子中我具有评论系统并且它有效,我有此PHP代码来计算我在评论和ID中有多少相同的字段。 这是代码:

    <?php
$revposid = $res['content_id'];
$pos="SELECT review FROM comments WHERE review='positive' and postid=$revposid";
$neg="SELECT review FROM comments WHERE review='negative' and postid=$revposid";
$neu="SELECT review FROM comments WHERE review='neutral' and postid=$revposid";
if ($result=mysqli_query($_db,$pos)){$rowcountpos=mysqli_num_rows($result);}
if ($result=mysqli_query($_db,$neg)){$rowcountneg=mysqli_num_rows($result);}
if ($result=mysqli_query($_db,$neu)){$rowcountneu=mysqli_num_rows($result);}
?>



<div class="reviews" id="reviews">
    <span class="good"><b class="fa fa-thumbs-up"></b><?php echo $rowcountpos ?></span> 
    <span class="neutral"><b class="icon-thumbs-up"></b><?php echo $rowcountneu ?></span> 
    <span class="bad"><b class="fa fa-thumbs-down"></b><?php echo $rowcountneg ?></span>
</div>

以及当我尝试使用相同的代码

$revposid = $cont['content_id'];
        $pos="SELECT content_id FROM user_content_like WHERE content_id=$revposid";
        if ($result=mysqli_query($_db,$pos)){$rowcountpos=mysqli_num_rows($result);}

在其他脚本中,我有喜欢系统,它应该显示帖子的所有喜欢和喜欢总数,但是当我使用它时,它显示的数据量是无限的,我不知道为什么。这是完整代码,我希望获得一些帮助或解释。

<?php $ususername = $_GET['user_username'];$sql = "SELECT * FROM user_details WHERE user_username='$ususername'";$usresult = mysqli_query($_db,$sql);?>
<?php if( ! mysqli_num_rows($usresult) ) {
            echo " Ooops? <br> <br>User <b>".$_GET["user_username"]."</b> doesn't exist.";
        } else {
            while($usrow = mysqli_fetch_array($usresult,MYSQLI_BOTH)) {?>







<?php


$current_user = $usrow['user_id'];
if ($_db->connect_error) {
    die("Connection failed: " . $_db->connect_error);
} 

$sql = "SELECT * FROM user_content_like WHERE user_id=$current_user ORDER BY date_added DESC;";
$result = $_db->query($sql);

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$content_id = $row['content_id'];


$sql1 = "SELECT * FROM content WHERE content_id=$content_id";
$result1 = $_db->query($sql1);      

    if ($_SESSION['user_id'] == $usrow['user_id']) {$output = '
    <button type="button" class="unlike_button" onclick="unlike(this);" name="like_button" data-content_id="'.$row["content_id"].'" ><i class="fa fa-minus"></i></button>
    ';} else {
        $output = '';
    }


    while($cont = $result1->fetch_assoc()) {

        $revposid = $cont['content_id'];
        $pos="SELECT content_id FROM user_content_like WHERE content_id=$revposid";
        if ($result=mysqli_query($_db,$pos)){$rowcountpos=mysqli_num_rows($result);}

        echo '
        <div class="community-feed-thread"> 
            <div class="community-icon-thread"></div> 
            <div class="community-comments-thread">'.$output.'</div> 
                <a href="'.$cont["content_id"].'" class="community-title-thread"><h3>'.$cont["title"].'</h3>
                    <span class="likes-desc"> Total likes:  '.$rowcountpos.'</span>
                </a>

        </div>
        ';
    }
}}
    else {
        echo " hmmmmmmmmmmmm.<Br><br>". $usrow["user_username"]." doesn't like anything. ";
    }

$_db->close();
?> 

<?php }}?>

this is how i want it to look

This is how it looks

1 个答案:

答案 0 :(得分:0)

您对代码中的不同结果集重复使用了相同的变量...

$result = $_db->query($sql);

if ($result=mysqli_query($_db,$pos))

您需要确保只使用一次变量名,否则它可能在其他循环中产生副作用。