$xdate = "2010-10-01 00:00:00";
$zdate = "2010-10-06 24:00:00";
//time 字段格式同上 类型 timestamp 默认 CURRENT_TIMESTAMP
$sql = "select * from orders where time>=$xdate and time<=$zdate order by id desc";
while ($rs = mysql_fetch_array(mysql_query($sql))){
//……省略
}
输出的时候不能正确判断。谁能告诉我应该咋写
在SQL语句中变量xdate,zdate加上单引号,能输出,但是输出的是全部,没有根据规定的时间输出
而,数据库存的是什么类型?也是这样类型??不是 stamp 类型吗???
$sql = "select * from orders where time>='$xdate' and time<='$zdate' order by id desc";改成这样试一下
+----------+-----------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-----------+------+-----+-------------------+-------+
| id | int(11) | YES | | NULL | |
| testtime | timestamp | NO | | CURRENT_TIMESTAMP | |
+----------+-----------+------+-----+-------------------+-------+
2 rows in set (0.00 sec)mysql> select * from test;
+------+---------------------+
| id | testtime |
+------+---------------------+
| 1 | 2010-10-06 12:14:18 |
| 2 | 2010-10-06 12:14:24 |
| 3 | 2010-10-06 12:17:00 |
| 4 | 2010-10-06 12:17:25 |
| 5 | 2010-10-07 13:05:14 |
+------+---------------------+
5 rows in set (0.00 sec)mysql> select * from test where testtime>='2010-10-06 12:14:19' and testtime<='2
010-10-07 13:05:13';
+------+---------------------+
| id | testtime |
+------+---------------------+
| 2 | 2010-10-06 12:14:24 |
| 3 | 2010-10-06 12:17:00 |
| 4 | 2010-10-06 12:17:25 |
+------+---------------------+
3 rows in set (0.00 sec)SQL: select * from test where testtime>=2010-10-06 12:14:1' and testtime<=2
010-10-07 13:05:13
去掉单引号
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '12:14:1' and testtime<=2
010-10-07 13:05:13' at line 1
//……省略
}你这样子写出来是不是一个死循环 出来全部数据而且全部数据都是相同的?
改成
$result=mysql_query($sql);
while ($rs = mysql_fetch_array($result)){
...
}看下两个变量不加单号是不是报错了
现在我自己搞定了,变量需要加引号。而且我之前的错误是自己大意了,我输出的格式1日是2010-10-1而不是2010-10-01 ,就是这个地方导致错误
非常感谢你的解答。