运行while循环以显示所有行,但它只显示一行

时间:2016-04-09 19:26:38

标签: php mysql

我正在尝试执行while循环来显示有关已标记帖子的数据。我目前以Conor身份登录,他已标记了2个帖子(flagged_posts字段中有2行)。但是当我执行下面的代码时,只有一个div是echo,而两个应该是echo,因为他标记了两个帖子。

这是我的方法:

<?php
// getting id of logged in user, need this to see all the flagged posts from logged in user.
$get_uid = mysqli_query ($connect, "SELECT id FROM users WHERE username = '$user' ");
        $get_d = mysqli_fetch_assoc($get_uid);
            $user_ident = $get_d ['id'];

// using the above id and getting all the flagged posts which have been flagged by the id obtained
$flagged_by2 = mysqli_query ($connect, "SELECT * FROM flagged_posts WHERE flagged_by_id = '$user_ident'");
        $num_of_flagged_posts = mysqli_num_rows ($flagged_by2);
        while ($getting_d = mysqli_fetch_assoc ($flagged_by2)){
            $flagged_thought_id = $getting_d['thought_id'];
        }

$query = mysqli_query($connect, "SELECT * FROM user_thoughts WHERE id='$flagged_thought_id' ORDER BY id DESC LIMIT {$start}, {$limit}");
while ($row = mysqli_fetch_array($query)) {
    $thought_id      = $row['id'];
    $message_content = $row['message'];
    $date_of_msg     = $row['post_details'];
    $thoughts_by     = $row['added_by'];
    $attachent       = $row['attachment'];
    $shared          = $row['shared'];

    echo 
        "   <div class='message_wrapper'>
                <p> Details would be displayed here </p>
            </div>";
} //while closed

修改

分页(采用MarcinNabiałek方法):

        $flagged_by2 = mysqli_query ($connect, "SELECT * FROM flagged_posts WHERE flagged_by_id = '$user_ident'");
        $flagged_ids = [];  
        $num_of_flagged_posts = mysqli_num_rows ($flagged_by2);
        while ($getting_d = mysqli_fetch_assoc ($flagged_by2)){
            $flagged_ids[] = $getting_d['thought_id'];
        }
        if (!$flagged_ids) {
            $flagged_ids[] = 0;
        }


            // Pagination - Load more content when bottom of page hit.  
            $limit = 10;

            $page = (int) (!isset($_GET['p'])) ? 1 : $_GET['p'];
            # sql query
            $query = mysqli_query($connect, "SELECT * FROM user_thoughts WHERE id IN " . implode(', ', $flagged_ids)." ORDER BY id DESC");
            # find out query start point
            $start = ($page * $limit) - $limit;
            # query for page navigation
            if(mysqli_num_rows($query) > ($page * $limit) ){
              $next = ++$page;
            }
            //$query = mysqli_query($connect, "SELECT * FROM user_thoughts WHERE id='$flagged_thought_id' ORDER BY id DESC LIMIT {$start}, {$limit}");
$query = mysqli_query($connect, "SELECT * FROM user_thoughts WHERE id IN " . implode(', ', $flagged_ids)." ORDER BY id DESC LIMIT {$start}, {$limit}");
            if (mysqli_num_rows($query) < 1) {
              header('HTTP/1.0 404 Not Found');
              echo "<div class='nothing_to_say'>
                        $ufirstname has no flagged posts.
                    </div>";
              exit();
            }

有了上述内容,我得到了两个相同的错误:

mysqli_num_rows()期望参数1为mysqli_result - 在线:

if(mysqli_num_rows($query) > ($page * $limit) ){ 

if (mysqli_num_rows($query) < 1) {

如果我评论这两行,那么我会收到另一个错误: mysqli_fetch_array()期望参数1为mysqli_result,在线:

while ($row = mysqli_fetch_array($query)) {

1 个答案:

答案 0 :(得分:0)

这里的问题是:

$flagged_by2 = mysqli_query ($connect, "SELECT * FROM flagged_posts WHERE flagged_by_id = '$user_ident'");
        $num_of_flagged_posts = mysqli_num_rows ($flagged_by2);
        while ($getting_d = mysqli_fetch_assoc ($flagged_by2)){
            $flagged_thought_id = $getting_d['thought_id'];
        }

$query = mysqli_query($connect, "SELECT * FROM user_thoughts WHERE id='$flagged_thought_id' ORDER BY id DESC LIMIT {$start}, {$limit}");

为同一个变量设置循环值,你应该在这里有数组。因此,您应该将此代码重写为:

$flagged_by2 = mysqli_query ($connect, "SELECT * FROM flagged_posts WHERE flagged_by_id = '$user_ident'");
        $flagged_ids = [];  
        $num_of_flagged_posts = mysqli_num_rows ($flagged_by2);
        while ($getting_d = mysqli_fetch_assoc ($flagged_by2)){
            $flagged_ids[] = $getting_d['thought_id'];
        }
        if (!$flagged_ids) {
            $flagged_ids[] = 0;
        }

$query = mysqli_query($connect, "SELECT * FROM user_thoughts WHERE id IN " . implode(', ', $flagged_ids)." ORDER BY id DESC LIMIT {$start}, {$limit}");

但是,您只能在显示注释时进行一次查询,此外您应该将PDO与预处理语句一起使用 - 现在您的代码很容易被SQL注入。