dongpo2458 2014-02-28 05: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 06: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;
    
    }
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部