2014-02-28 22 views
1

我正在製作預訂系統,因此我需要能夠在讓用戶預訂前查看是否有時隙(我的功能還在前後添加了1個時隙以檢查涵蓋旅行時間等)在phpmyamdin中查詢但在腳本中不能查詢

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; 

} 

輸出

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

現在bookings還有就是正在使用該日期時間段4.然後我試圖在phpMyAdmin

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

基本上是相同的查詢(提交相同的變量),但回報也可能隨着1正確的響應。這讓我覺得我的代碼有些問題是我看不到的。

+0

可能:bookings.date = 「2014年2月15日」 – Hackerman

+0

它是在2014年2月15日數據庫。這會有所作爲嗎? – Harry

+0

您是否將您的日期存儲爲varchar ?? .... holy molly ... – Hackerman

回答

0

這裏的問題是:

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; 

} 

這樣你的查詢看起來像:

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

當然答案是零。

改變你的代碼是這樣的:

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; 

} 

或者這樣說:

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; 

} 
相關問題