我试图在数据库的“用户”列中显示所有用户,但未显示任何内容。我想根据获得的喜欢次数对用户进行排名。我的查询对我来说看起来不错,但没有显示任何内容。没有警告,错误或我需要的输出。有人可以告诉我我在做什么错吗?我已经检查了所有答案约一个小时,然后尝试使用已经准备好的语句。但是什么都行不通。
这是整个页面:
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
error_reporting(-1);
require 'includes/header.php';
$stmt = $con->prepare('SELECT username, num_likes FROM users WHERE username = ? ORDER BY num_likes DESC ');
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($username, $num_likes);
// output data of each row
while ($stmt -> fetch()) {
echo '<div id="rcorners2">';
echo '<p id="p2">' . $username . '</p>';
echo '<p id="p4">' . $num_likes . '</p>';
echo'</div>';
echo'</br>';
}
?>
<!DOCTYPE html>
<html>
<head>
<title>User's Ranks</title>
<style type="text/css">
#rcorners2 {
border-radius: 5px;
border: 2px solid #8c8c8c;
padding: 20px;
width: 500px;
word-wrap: break-word;
height: 200px;
overflow-x: hidden;
margin-left: 350px;
}
#p2 {
float:left;
color: #000000;
margin-top: -10;
font-size: 20px;
}
#p4 {
float:left;
color: #000000;
margin-top: -1%;
font-size: 20px;
}
</style>
</head>
<body>
</body>
</html>
答案 0 :(得分:1)
在输出HTML <!DOCTYPE html>
和HTML Document的其他基本部分之前,您还需要计算查询结果。因此,即使您有任何行,它们也不会出现在文档中
如果您在文档<body>
中输出信息,它将显示在页面上
<!DOCTYPE html>
<html>
<head>
<title>User's Ranks</title>
<style type="text/css">
#rcorners2 {
border-radius: 5px;
border: 2px solid #8c8c8c;
padding: 20px;
width: 500px;
word-wrap: break-word;
height: 200px;
overflow-x: hidden;
margin-left: 350px;
}
#p2 {
float:left;
color: #000000;
margin-top: -10;
font-size: 20px;
}
#p4 {
float:left;
color: #000000;
margin-top: -1%;
font-size: 20px;
}
</style>
</head>
<body>
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
error_reporting(-1);
require 'includes/header.php';
$stmt = $con->prepare('SELECT username, num_likes
FROM users WHERE username = ?
ORDER BY num_likes DESC ');
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($username, $num_likes);
// output data of each row
while ($stmt -> fetch()) {
echo '<div id="rcorners2">';
echo '<p id="p2">' . $username . '</p>';
echo '<p id="p4">' . $num_likes . '</p>';
echo'</div>';
echo'</br>';
}
?>
</body>
</html>
您还说过,您希望所有用户都支付,因此查询可能还需要更改,以使所有用户只能获得一个。
$result = $con->query('SELECT username, num_likes
FROM users
ORDER BY num_likes DESC ');
// output data of each row
while ($row = $result->fetch_assoc()) {
echo '<div id="rcorners2">';
echo '<p id="p2">' . $row['username'] . '</p>';
echo '<p id="p4">' . $row['num_likes'] . '</p>';
echo'</div>';
echo'</br>';
答案 1 :(得分:-1)
您需要先初始化数据库连接。
例如$ mysqli =新的mysqli(“ localhost”,“ my_user”,“ my_password”,“ my_db”); 请参阅:https://www.w3schools.com/php/func_mysqli_connect.asp