共有 mysql 个结果显示不正确

The total number of mysql results not displaying properly

我正在开发一个总页面浏览系统,每次有人访问一个页面时,它都会添加当前数据和他们的 IP 地址。当我去显示特定日期的总行数时,网站只显示 'Rows' 而没有数字。 代码:

<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
$date = date("Y-m-d-h-m-s");
$ip = $_SERVER['REMOTE_ADDR'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 
$date = date("Y-m-d");
$result = mysqli_query("SELECT FROM page_views WHERE date = $date");
$num_rows = mysqli_num_rows($result);

echo "$num_rows Rows\n";

$conn->close();
?>

谁能指出问题所在。

您的查询失败了,但您并不知道,因为您没有检查错误。如果你这样做了,你会看到语法错误,因为你从 SELECT 子句中省略了 * 并且你的日期没有用引号引起来。

变化:

$result = mysqli_query("SELECT * FROM page_views WHERE date = $date");

至:

$result = mysqli_query("SELECT * FROM page_views WHERE date = '$date'");