2015-09-06 207 views
0

我正在使用總頁面查看系統,每次有人訪問某個頁面時,它都會添加當前數據及其ip地址。當我去顯示特定日子的總行數時,站點只顯示沒有數字的'行'。 代碼:mysql結果總數顯示不正確

<?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(); 
?> 

請問某人請指出發生了什麼問題。

回答

1

您的查詢失敗,但您不知道它,因爲您不檢查錯誤。如果你這樣做,你會看到一個語法錯誤,因爲你從SELECT子句中省略了*,並且你的日期沒有用引號引起來。

變化:

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

到:

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