Php SQL选择不输出数据

Php SQL选择不输出数据,php,mysql,Php,Mysql,一开始,我对PHP/SQL有点茫然,不知道如何进行错误测试,所以请原谅我对问题的确切性质含糊其辞。继续 我有一些代码可以从SQL表中获取类别ID、名称和描述,并将结果保存到变量中。这是通过语句准备来完成的,以避免SQL注入的可能性。然后将该值输入一些PHP,PHP检查查询是否有响应,如果有,则将其打印到表中 <?php //create_cat.php include_once (__DIR__ . '/../includes/db_connect.php'); include_once

一开始,我对PHP/SQL有点茫然,不知道如何进行错误测试,所以请原谅我对问题的确切性质含糊其辞。继续

我有一些代码可以从SQL表中获取类别ID、名称和描述,并将结果保存到变量中。这是通过语句准备来完成的,以避免SQL注入的可能性。然后将该值输入一些PHP,PHP检查查询是否有响应,如果有,则将其打印到表中

<?php

//create_cat.php
include_once (__DIR__ . '/../includes/db_connect.php');
include_once (__DIR__ . '/../includes/functions.php');

include_once (__DIR__ . '/header.php');
ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(-1);
$stmt = "SELECT
            cat_id,
            cat_name,
            cat_description
        FROM
            categories";
if (login_check($mysqli) == true) {
    if(!$result = $mysqli->query($stmt)){
        echo 'The categories could not be displayed, please try again later.';
    } else {
        if ($result->num_rows === 0) {
            echo 'No categories defined yet.';
        } else {
            //prepare the table
            echo '<table border="1">
              <tr>
                <th>Category</th>
                <th>Last topic</th>
              </tr>';

            while ($row = $result->fetch_assoc()) {
                echo '<tr>';
                echo '<td class="leftpart">';
                echo '<h3><a href="category.php?id">' . $row['cat_name'] . '</a></h3>' . $row['cat_description'];
                echo '</td>';
                echo '<td class="rightpart">';
                echo '<a href="topic.php?id=">Topic subject</a> at 10-10';
                echo '</td>';
                echo '</tr>';
            }
        }
    }
} else {
    echo <<<error
    <p>
    <span class="error">You are not authorized to access this page.</span> Please <a href="../index.php">login</a>.
    </p>
error;
}
include_once (__DIR__ . '/footer.php');
?>


然而,表SQL表肯定有值,但PHP只输出:“类别无法显示,请稍后再试。”

好,开始工作。我删除了$stmt_prep命令,确保只使用mysqli命令,并修复了一些语法错误。代码仍然破坏了HTML,但我所问的问题已经解决。

如果要保持面向对象的风格,主要错误是在获取结果之前关闭语句。无论如何,PHP都会在脚本末尾释放语句,但一旦调用
$stmt->close()您将无法从查询中读取任何数据。由于PHP是通过引用进行复制的,
$result=$stmt
不复制数据,它只引用同一个closed语句

fetch语法也不是您所拥有的语法:您需要使用例如
$stmt->bind\u result($name,$description)绑定一些占位符变量$stmt->fetch()
,而不是fetch\u assoc

我不清楚
login\u check
到底做了什么,但在我看来,您可能希望更早地进行此检查,以便在用户未经授权的情况下不会执行查询

您的原始代码最终会看起来像:

<?php

if (login_check($mysqli) == true) {
    $stmt = $mysqli->prepare($prep_stmt);
    if ($stmt) {
        $stmt->execute();
        $stmt->store_result();
        if ($stmt->num_rows == 0) {
            echo 'No categories defined yet.';
        } else {
            //prepare the table
            echo '<table border="1">
              <tr>
                <th>Category</th>
                <th>Last topic</th>
              </tr>';

            $stmt->bind_result($name, $description);
            while ($stmt->fetch()) {
                echo '<tr>';
                echo '<td class="leftpart">';
                echo '<h3><a href="category.php?id">' . $name . '</a></h3>'
                  . $description;
                echo '</td>';
                echo '<td class="rightpart">';
                echo '<a href="topic.php?id=">Topic subject</a> at 10-10';
                echo '</td>';
                echo '</tr>';
            }
        }
    } else {
        echo 'The categories could not be displayed, please try again later.';
    }
} else {
    echo <<<error

    <p>
        <span class="error">You are not authorized to access this page.</span> Please 
        <a href="../index.php">login</a>.

     </p>
error;
}

在PHP代码顶部添加以下行,它们将帮助您调试问题:
ini\u集('display\u errors',1);ini设置(“显示启动错误”,1);错误报告(-1)
另请参阅
存储结果()
的文档,因为您应该将值存储到变量中,然后分析变量。你的陈述似乎有些混乱。请参阅有关使用mysqli的教程。您的代码混合了
mysqli_
mysql_
函数。不要那样做。仅使用
mysqli\uu
函数。您认为此时是否有必要使用
prepare()
?此命令没有在我的数据库中插入任何内容,并且所有数据库输入都已清理。@spencer7593,已修复。我将
mysql\u num\u rows($result)==0
替换为
$result->num\u rows==0
,将
$row=mysql\u fetch\u assoc($result)
替换为
$row=$result->fetch\u assoc()