Php 未定义变量:_post_id和警告:mysqli_fetch_assoc()要求参数1为mysqli_result,布尔值

Php 未定义变量:_post_id和警告:mysqli_fetch_assoc()要求参数1为mysqli_result,布尔值,php,sql,mysqli,Php,Sql,Mysqli,注意:未定义变量:第27行C:\xampp\htdocs\renda\post.php中的\u post\u id 警告:mysqli_fetch_assoc()希望参数1是mysqli_结果,布尔值在第30行的C:\xampp\htdocs\renda\post.php中给出 这是27号线 $query ="SELECT * FROM posts WHERE post_id = {$the_post_id}"; 这是30号线 while ($row = mysqli_fe

注意:未定义变量:第27行C:\xampp\htdocs\renda\post.php中的\u post\u id

警告:mysqli_fetch_assoc()希望参数1是mysqli_结果,布尔值在第30行的C:\xampp\htdocs\renda\post.php中给出

这是27号线

$query ="SELECT * FROM posts WHERE post_id = {$the_post_id}";
这是30号线

while ($row = mysqli_fetch_assoc($select_all_posts_query)) {
这是我的PHP代码

<?php

if(isset($_GET['p_id'])) {
    $the_post_id = $_GET['p_id'];
}


$query ="SELECT * FROM posts WHERE post_id = {$the_post_id}";
$select_all_posts_query = mysqli_query($connection, $query);

while ($row = mysqli_fetch_assoc($select_all_posts_query)) {
    $post_id = $row['post_id'];
    $post_title = $row['post_title'];
    $post_author = $row['post_author'];
    $post_date = $row['post_date'];
    $post_image = $row['post_image'];
    $post_content = $row['post_content'];
如果p_id不存在,可以通过不执行所有代码来防止第一个“通知”错误。第二个错误是因为
mysqli\u query
可以返回false(请参阅)

我可能会这样做:

<?php

    if (isset($_GET['p_id'])) {

        $the_post_id = $_GET['p_id'];

        $query = "SELECT * FROM posts WHERE post_id = $the_post_id";
        $select_all_posts_query = mysqli_query($connection, $query);

        if ($select_all_posts_query) {
            while ($row = mysqli_fetch_assoc($select_all_posts_query)) {
                $post_id = $row['post_id'];
                $post_title = $row['post_title'];
                $post_author = $row['post_author'];
                $post_date = $row['post_date'];
                $post_image = $row['post_image'];
                $post_content = $row['post_content'];
            }
        }
        else {
            // handle error condition (the query failed)
        }
    }
    else {
        // handle p_id not being in $_GET
    }

?>


1检查您是否在
$中获取任何参数\u GET
2您的连接字符串丢失。请阅读并向我们显示您的
标记的HTML。我们需要知道您认为您正在传递给脚本的内容,$\u-GET
$\u-GET['p\u-id']
未在此处设置,查询已执行<代码>打印($_GET['p_id'])还添加一个
print\r($\u GET),然后显示它返回的内容