我在我的数据库中有一个学生表,我正在尝试显示所有记录。我按照了如何查询的教程,但它不能与我迄今为止所做的工作有关。解决这个问题的方法。 我的代码:
?php
$db = new mysqli('localhost', 'root', 'root', 'sportsday') or die("Unable to connect");
//echo "Connected";
error_reporting(0);
if ($result = $db->query("SELECT * FROM student")) {
if ($result->num_rows) {
$count= $result->num_rows;
if ($count > 0) {
while ($row = $result->fetch_assoc()); {
echo $row['surname'], " ", $row['forename'];
}
//$result-> free();
}
}
}
答案 0 :(得分:1)
首先,在你的开发环境中,你可以通过php.ini或程序化的方式打开你的错误并调整你的错误报告:
error_reporting(E_ALL);
ini_set('display_errors', 1);
这样可以让您看到正在发生的错误并提醒您注意问题。现在已经说过立即突出的东西是语法错误......
点(.
)是字符串连接运算符而不是逗号(,
)所以
echo $row['surname'], " ", $row['forename'];
应该是:
echo $row['surname'] . " " . $row['forename'];
您的while
循环中有一个错误的分号...
while ($row = $result->fetch_assoc()); {
应该是:
while ($row = $result->fetch_assoc()) {
所以把它们放在一起:
error_reporting(E_ALL);
ini_set('display_errors', 1);
$db = new mysqli('localhost', 'root', 'root', 'sportsday') or die("Unable to connect");
if ($result = $db->query("SELECT * FROM student")) {
if ($result->num_rows) {
$count = $result->num_rows;
if ($count > 0) {
while ($row = $result->fetch_assoc()) {
echo $row['surname'] . " " . $row['forename'];
}
//$result-> free();
}
}
}