dongpo2458 2014-02-28 13:42
浏览 58
已采纳

查询在phpmyadmin中工作,但不在脚本中

I am making a booking system and therefor I need to be able to check to see if timeslots are available before letting a user book (my function also adds 1 timeslot before and after to check for to cover traveling time etc.)

function timeSlotAvailable($date, $time){

    $timeslots = array($time - 1, $time, $time + 1);
    $slots = join(',',$timeslots); 

    $STH = $this->database->prepare("SELECT COUNT(*) FROM bookings WHERE bookings.date = :bdate AND bookings.slot IN (:ids)");
    $STH->execute(array(":bdate"=>$date, ":ids"=>$slots));
    $data = $STH->fetchColumn();
    return "checking date:".$date." for slots ".$slots." the count is ".$data;

}

outputs

checking date:02/15/2014 for slots 3,4,5 the count is 0

Now in bookings There is a slot that is being used for that date on time slot 4. I then try this query in phpmyadmin

SELECT COUNT(*) FROM bookings WHERE bookings.date = "02/15/2014" AND bookings.slot IN (3,4,5)

Which is essentially the same query ( same variables being submitted ) but returns with the correct response of 1. This makes me think there is something wrong with my code that I cannot see.

  • 写回答

1条回答 默认 最新

  • dtwknzk3764 2014-02-28 14:16
    关注

    Here is the problem:

    function timeSlotAvailable($date, $time){
    
    $timeslots = array($time - 1, $time, $time + 1); 
    $slots = join(',',$timeslots); //bad
    
    $STH = $this->database->prepare("SELECT COUNT(*) FROM bookings WHERE bookings.date = :bdate AND bookings.slot IN (:ids)");
    $STH->execute(array(":bdate"=>$date, ":ids"=>$slots)); //here is the problem
    $data = $STH->fetchColumn();
    return "checking date:".$date." for slots ".$slots." the count is ".$data;
    
    }
    

    This way your query looks like:

    SELECT COUNT(*) 
    FROM bookings WHERE bookings.date ='02/15/2014' 
    AND bookings.slot IN (3) //Just took one, oops
    

    And of course the answer is zero.

    Change your code this way:

    function timeSlotAvailable($date, $time){
    
    $timeslots = array($time - 1, $time, $time + 1); 
    
    $STH = $this->database->prepare("SELECT COUNT(*) FROM bookings WHERE bookings.date = :bdate AND bookings.slot IN (:ids)");
    $STH->execute(array(":bdate"=>$date, ":ids"=>$timeslots)); 
    $data = $STH->fetchColumn();
    return "checking date:".$date." for slots ".$slots." the count is ".$data;
    
    }
    

    Or this way:

    function timeSlotAvailable($date, $time){
    
    $timeslots = array($time - 1, $time, $time + 1); 
    $slots = join(',',$timeslots); 
    
    $STH = $this->database->prepare("SELECT COUNT(*) FROM bookings WHERE bookings.date = :bdate AND bookings.slot IN (".$slots.")");
    $STH->execute(array(":bdate"=>$date)); 
    $data = $STH->fetchColumn();
    return "checking date:".$date." for slots ".$slots." the count is ".$data;
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端