嘗試根據用戶給出的開始日期和結束日期顯示MySQL DB的結果。以下是代碼:PHP + MySQL查詢:意外的結果(僅針對少量結果)
//Getting Values From Other Page
$start_date = date("Y-m-d", strtotime($_GET["date3"]));
$end_date = date("Y-m-d", strtotime($_GET["date4"]));
//Server Connection Info
$server = "server_name";
$db_user = "username";
$db_pass = "pass";
$db_name = "dbname";
//Server Connection + Query
$link = mysql_connect($server,$db_user,$db_pass);
if(!$link)
{
die("Could Not Connect:".mysql_error());
}
mysql_select_db($db_name, $link) or die('Can\'t use db:'. mysql_error());
if ($start_date == $end_date){
$query = "SELECT col_a, col_b, col_c, col_d FROM main WHERE date='".$start_date."'";
}
else {
$query = "SELECT col_a, col_b, col_c, col_d FROM main WHERE date BETWEEN '".$start_date."' AND '".$end_date."'";
}
$result = mysql_query($query,$link) or die('Query Error'.mysql_error());
?>
<!-- HTML CODE STARTS HERE -->
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="qc_style.css" />
<link href='http://fonts.googleapis.com/css?family=Days+One' rel='stylesheet' type='text/css' />
</head>
<body>
<?php
$row = mysql_fetch_assoc($result);
$row_count = mysql_num_rows($result);
echo $row;
echo $row_count;
if($row_count!=0){
echo "<table>";
while($row = mysql_fetch_assoc($result))
{
echo "<tr><td>";
foreach($row as $value)
{
echo $value;
echo "</td><td>";
}
echo "<a href=\"qc_campedit.php?id=".$row['call_id']."\">Edit</a></td></tr>";
}
echo "</table>";
} //end of if statement
else {echo " Sorry, No records match your query";}
?>
</body>
</html>
我有兩行數據在我的數據庫日期2011-08-25和一行數據的日期2011-08-24。
- 當我指定開始日期和結束日期爲2011-08-24時,我沒有得到 響應。
- 但是,當我指定開始日期和結束日期爲2011-08-25時,我的 只得到一行數據屬於2011-08-25
- 當我指定開始日期爲2011-08-24結束日期爲2011-08-25 ,我得到屬於2011-08-25的兩行數據和屬於2011-08-25的行。
- 然而,當我指定的開始日期和結束日期爲2011-08-23,我得到的所有的20行數據屬於2011-08-23
注:陣列的數量在$result
的特定時間範圍內顯示正確。即2011-08-24的$ row_count = 1和2011-08-24的$ row_count = 2
如何解決此問題?提前致謝。
什麼是日期列的數據類型? –
日期列的數據類型爲DATE –