從PHP/SQL開始,我在黑暗中磕磕絆絆,真的不知道如何進行bug測試,所以請原諒我對問題確切性質的模糊性。繼續。SQL選擇不輸出數據
我有一些代碼從SQL表中獲取類別ID,名稱和描述,並將結果保存到變量中。這是通過準備避免SQL注入的可能性來完成的。然後將該值輸入到一些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只輸出:「類別無法顯示,請稍後再試。」
以下行添加到你的PHP代碼的頂部,他們應該幫你調試的問題:'的ini_set(「display_errors設置」,1); ini_set('display_startup_errors',1); error_reporting(-1);' –
另請參閱'store_result()'[here](https://php.net/manual/en/mysqli.store-result.php)的文檔,因爲您應該存儲該值變成一個變量,然後分析變量。你的發言似乎有些混亂。請參閱[這裏](http://codular.com/php-mysqli)關於使用mysqli的教程。 –
你的代碼混合了'mysqli_'和'mysql_'的功能。不要這樣做。僅使用'mysqli_'函數。 – spencer7593