一切似乎都很好,只記得你提供的信息。我們將不得不看到整個代碼來找到錯誤。也許這是一個MySQL字段類型的問題,或者它是關於你發送查詢到MySQL的方式。
我有一個工作的例子在這裏與您的表的最小版本。我希望它有幫助!
電冰箱表字段:
mysql> desc fridge;
+----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| food | varchar(20) | YES | | NULL | |
| date_purchased | date | YES | | NULL | |
+----------------+------------------+------+-----+---------+----------------+
(注意在date_purchased
字段date
類型)。
編輯:如果您對該字段使用date
或datetime
類型,則此示例適用。
冰箱表的內容:
mysql> select * from fridge;
+----+-----------+----------------+
| id | food | date_purchased |
+----+-----------+----------------+
| 1 | hamburger | 2016-04-28 |
| 2 | pizza | 2016-04-12 |
| 3 | salad | 2016-05-10 | <-- future date
| 4 | fruit | 2016-05-04 | <-- future date
+----+-----------+----------------+
PHP代碼(using語句來查詢數據庫):
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// prepare and bind
$stmt = $conn->prepare("select food from fridge where date_purchased < ?");
$stmt->bind_param("s", $date);
// set parameters and execute
$date = date("Y-m-d"); // NOTE: $date stores the current date
echo "<p>Food with date < $date</p>";
$stmt->execute();
// bind variables to prepared statement
$stmt->bind_result($food);
// fetch values (here you can do whatever you want with results)
while ($stmt->fetch()) {
echo "<p>$food</p>";
}
$stmt->close();
$conn->close();
結果(我們不希望健康的食品現在...):
食品與日期< 2016年5月1日
漢堡
比薩餅
請問您可以添加您的SQL查詢和PHP日期格式代碼? – nanocv
@nanocv見上文。 – Brinley