$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加上单引号,能输出,但是输出的是全部,没有根据规定的时间输出

解决方案 »

  1.   

    你的data与数据库的time 不同一类型啊,乍比较???
      

  2.   

    你要用数据库的值与: 2010-10-01 00:00:00 比较
    而,数据库存的是什么类型?也是这样类型??不是 stamp 类型吗???
      

  3.   

    建议你转化成unix时间戳来比较
      

  4.   

    是timestamp,默认 CURRENT_TIMESTAMP;也就是插入数据的时候,自动写入当前时间
      

  5.   

    strtotime() 用了,转换有值,但是还是不执行判断
      

  6.   


    $sql = "select * from orders where time>='$xdate' and time<='$zdate' order by id desc";改成这样试一下
      

  7.   

    mysql> desc test;
    +----------+-----------+------+-----+-------------------+-------+
    | 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 
      

  8.   

    while ($rs = mysql_fetch_array(mysql_query($sql))){
     //……省略
    }你这样子写出来是不是一个死循环 出来全部数据而且全部数据都是相同的?
    改成 
    $result=mysql_query($sql);
    while ($rs = mysql_fetch_array($result)){
    ...
    }看下两个变量不加单号是不是报错了
      

  9.   

    哦,我知道我那样写会死循环,我是这样写例子方便就顺手写了,我自己的程序是把Mysql_fetch_array和mysql_query分开写的
    现在我自己搞定了,变量需要加引号。而且我之前的错误是自己大意了,我输出的格式1日是2010-10-1而不是2010-10-01 ,就是这个地方导致错误
    非常感谢你的解答。