【问题标题】:mysqli_fetch_assoc not showing all resultsmysqli_fetch_assoc 未显示所有结果
【发布时间】:2017-09-28 06:44:19
【问题描述】:

我不明白为什么 mysqli_fetch_assoc 只显示一个结果而不是五个。我仔细检查了数据库,确定有 5 个条目..

如果我删除第二个 mysqli_fetch_assoc 则显示所有结果。

请注意:连接包括在上面..

任何意见和建议将不胜感激...谢谢!

<table class="table table-bordered table-hover">
    thead>
         <tr>
            <th>Id</th>
            <th>Author</th>
            <th>Title</th>
            <th>Category</th>
            <th>Status</th>
            <th>Image</th>
            <th>Tags</th>
            <th>Comments</th>
            <th>Date</th>
        </tr>
 </thead>
       <tbody>
            <?php 
               if(isset($_GET['delete'])){
                     $post_id = $_GET['delete'];   
                     $query = "DELETE FROM posts  WHERE post_id='$post_id'";
                     $delete_post_query = mysqli_query($con, $query);

            }
               $query = mysqli_query($con, "SELECT * FROM posts");
           while($row = mysqli_fetch_assoc($query)) {
                     $post_id = $row['post_id'];
                     $post_category_id = $row['post_category_id']; 
                     $post_title = $row['post_title']; 
                     $post_author = $row['post_author'];
                     $post_status = $row['post_status'];
                     $post_image = $row['post_image'];
                     $post_content = $row['post_content'];
                     $post_tags = $row['post_tags'];
                     $post_comment_count = $row['post_comment_count'];
                     $post_date = $row['post_date'];


                                    echo "<tr>";
                                    echo "<td>$post_id</td>";
                                    echo "<td>$post_author</td>";
                                    echo "<td>$post_title</td>";

                                $query = mysqli_query($con, "SELECT * FROM categories WHERE cat_id='$post_category_id'");

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

                                    $cat_id = $row['cat_id'];    
                                    $cat_title = $row['cat_title'];

                                    echo "<td>$cat_title</td>";

                                }

                                    echo "<td>$post_status</td>";
                                    echo "<td><img src='../images/$post_image' alt='image' width='100'></td>";
                                    echo "<td>$post_tags</td>";
                                    echo "<td>$post_comment_count</td>";
                                    echo "<td>$post_date</td>";
                                    echo "<td><a href='posts.php?delete=$post_id'>Delete</a></td>";
                                    echo "<td><a href='posts.php?source=edit_post&p_id=$post_id'>Edit</a></td>";
                                    echo "</tr>";

                                }

                                ?>

                            </tr>
                        </tbody>
                    </table>

【问题讨论】:

  • 危险:你很容易受到SQL injection attacks的影响,你需要defend你自己。
  • 我知道......!我将在最后解决所有安全问题..
  • 这就像盖房子,然后在最后解决地基问题。你最终会撞倒墙壁来解决你一开始就建立的问题。然后你只需要毫无意义地重建墙壁。一开始就做好。
  • @MattDomer 这一定是 StackOverflow 上常见的最糟糕的态度之一
  • 您编辑了问题以将 RESOLVED 放在顶部。这不是 Stackoverflow 的工作方式。请阅读How does accepting an answer work?

标签: php mysqli


【解决方案1】:

您正在嵌套数据库查询并重复使用相同的变量名

第一次围绕循环进行嵌套查询并覆盖 $query 变量。

您完成了内部循环,然后尝试从内部循环查询(您刚刚完成循环)中读取下一项。

不要循环使用变量名。

还可以考虑使用 JOIN 而不是多个查询。

【讨论】:

  • 是的...谢谢!
猜你喜欢
  • 2020-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多