dongya4089 2016-04-23 21:04
浏览 88

输入(datetime_local)字段中的MYSQL查询

I've got a problem with mysql query:

$req=mysql_query("
  SELECT Device_Type, Device_Number, sum(Ok) as Ok, sum(Rejected) as Rejected, sum(QC_Rejected) as QC_Rejected
  FROM `Iteration_TotalStats`
  where Line_Id = $line and Device_Number IN ($dev) and time > '$datest' and time < '$daten'
");

variables into "for" loop:

  • $datest=$_POST['date_start'];
  • $daten=$_POST['date_end '];

all works fine except time parameters with variable ($dates and $datee). Inputs here:

<input type="datetime-local" name="date_start" value="2016-04-19T09:00"></input>
<input type="datetime-local" name="date_end" value="2016-04-19T21:00"></input>

but when i type this date and time format direct in the mysql query like this:

time > '2016-04-19T09:00' and time < '2016-04-19T21:00'

it works fine. I have no idea why it doesn't work with variable. Please help.

  • 写回答

2条回答 默认 最新

  • dongzong5017 2016-04-23 21:37
    关注

    Just to put you on the richt track, and to show that PDO isn't that hard once you get used to it, I went ahead and converted your code to PDO:

    $qry = $db->prepare(
        'SELECT '
            . 'Device_Type, '
            . 'Device_Number, '
            . 'sum(Ok) as Ok, '
            . 'sum(Rejected) as Rejected, '
            . 'sum(QC_Rejected) as QC_Rejected '
        . 'FROM `Iteration_TotalStats` '
        . 'WHERE Line_Id = :line '
            . 'AND Device_Number IN ($dev) '
            . 'AND time > :start '
            . 'AND time < :end'
    );
    
    $qry->bindParam(':line', $line);
    $qry->bindParam(':start', $_POST['date_start']);
    $qry->bindParam(':end', $_POST['date_end']);
    
    $result = $qry->execute();
    

    You'll have to instantiate that $db variable first obviously, but that is just a PDO instance, which should be clear from the manual: http://php.net/manual/en/pdo.construct.php

    Let me know if this solves your problem. And do feel free to ask if anything is unclear.

    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图