我正在制作一个完整的页面浏览系统,每当有人访问某个页面时,它会添加当前数据及其IP地址。当我去显示特定日期的总行数时,网站只显示没有数字的“行”。 代码:
<?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();
?>
有人可以说明出了什么问题。
答案 0 :(得分:1)
您的查询失败但您不知道,因为您没有检查错误。如果你这样做,你会看到语法错误,因为你省略了SELECT子句中的$result = mysqli_query("SELECT * FROM page_views WHERE date = $date");
而你的日期没有引号。
变化:
$result = mysqli_query("SELECT * FROM page_views WHERE date = '$date'");
为:
<html>
<!-- google API reference -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<!-- my own script for search function -->
<center>
<form method="POST">
<input type="text" name="search" style="width:400px " placeholder="Search box" onkeyup="searchq();">
<div id="output">
</div>
</form>
</center>
<!-- instant search function -->
<script type="text/javascript">
function searchq(){
// get the value
var txt = $("input").val();
// post the value
if(txt){
$.post("search.php", {searchVal: txt}, function(result){
$("#search_output").html(result+"<div id=\"create\" onclick=\"creatq()\"><br>Not found above? Create.</div>");
});
}
else{
$("#search_output").html("");
}
};
function createq(){
// allert for test purpose: test if the txt has got by the createq function
alert(txt);
**$.post( "create_object.php",{creatVal:txt} );**
}
// if enter key pressed, redirect page to the first search result
$("#search").keypress(function(evt){
if (evt.which == 13) {
// find the first search result in DOM and trigger a click event
var redirectUrl = $('#search_output').find('a').first().attr('href');
alert(redirectUrl);
**window.location.href = "www.google.com";
window.location.href = "www.google.com";**
}
})
</script>
</html>