我似乎无法找到的PHP / SQL错误

时间:2015-05-24 13:55:11

标签: php sql

执行时,它总是声明“此时停车位不可用”,即使给出的数据库另有说明。我究竟做错了什么?可能还有其他错误,但我似乎无法超越这个错误。

$db = connect();
try {
    $db->beginTransaction();
    if(isWeekend($bookingDate)){
        $stmt = $db->prepare('SELECT bayid FROM parkbay p WHERE p.bayid=:bayID AND EXISTS (SELECT * FROM parkbay p WHERE p.bayid=:bayID AND(:parktime BETWEEN avail_wend_start AND avail_wend_end) AND (:parktime+:duration BETWEEN avail_wend_start AND avail_wend_end))');
    } else {
        $stmt = $db->prepare('SELECT bayid FROM parkbay p WHERE p.bayid=:bayID AND EXISTS (SELECT * FROM parkbay p WHERE p.bayid=:bayID AND(:parktime BETWEEN avail_wk_start AND avail_wk_end) AND (:parktime+:duration BETWEEN avail_wk_start AND avail_wk_end))');
    }
    $stmt->bindValue(':bayID', $bayID, PDO::PARAM_INT);
    $stmt->bindValue(':parktime', $bookingHour, PDO::PARAM_INT);
    $stmt->bindValue(':duration', $duration, PDO::PARAM_INT);
    $stmt->execute();
    $result = $stmt->fetch();
    $stmt->closeCursor();
    if ( !empty($result) ) {             
        $stmt2 = $db->prepare( "SELECT * FROM booking b JOIN parkbay p ON (p.bayid=b.bayid) WHERE b.bayid=:bayID2 AND b.bookingdate=:bookingDate2 AND (:parktime2 BETWEEN b.bookinghour AND b.bookinghour + b.duration) AND (:parktime2 + :duration2 BETWEEN b.bookinghour AND b.bookinghour + b.duration)" );
        $stmt2->bindValue(':bayID2', $bayID, PDO::PARAM_INT);
        $stmt2->bindValue(':bookingDate2', $bookingDate, PDO::PARAM_STR);
        $stmt2->bindValue(':parktime2', $bookingHour, PDO::PARAM_INT);
        $stmt2->bindValue(':duration2', $duration, PDO::PARAM_INT);
        $stmt2->execute();
        $result = $stmt2->fetchAll();
        $stmt2->closeCursor();
        if ( empty($result) ) {                 
            $stmt3 = $db->prepare("SELECT c.name, t.width, t.height, t.length FROM cartype t JOIN car c ON (c.make = t.make AND c.model = t.model) WHERE c.name=:carname AND EXISTS (
SELECT p.site, p.length, p.width, p.height FROM parkbay p WHERE p.bayid=:bayID3 AND t.width<=p.width AND t.length<=p.length AND t.height<=p.height)");
            $stmt3->bindValue(':bayID3', $bayID, PDO::PARAM_INT);
            $stmt3->bindValue(':carname', $car, PDO::PARAM_STR);
            $stmt3->execute();
            $result = $stmt3->fetch();
            $stmt3->closeCursor();
            if ( !empty($result) ) { 
                $stmt4 = $db->prepare('INSERT INTO booking (bookingid, bayid, bookingdate, bookinghour, duration, memberno, car) VAlUES (DEFAULT, :bayIDBooking, :bookingDateBooking, :bookingHourBooking, :durationBooking, :memberNoBooking, :carBooking) RETURNING bookingid');
                $stmt4->bindValue(':bayIDBooking',$bayID,PDO::PARAM_INT);
                $stmt4->bindValue(':bookingDateBooking',$bookingDate,PDO::PARAM_STR);
                $stmt4->bindValue(':bookingHourBooking',$bookingHour,PDO::PARAM_INT);
                $stmt4->bindValue(':durationBooking',$duration,PDO::PARAM_INT);
                $stmt4->bindValue(':memberNoBooking',$memberNo,PDO::PARAM_INT);
                $stmt4->bindValue(':carBooking',$car,PDO::PARAM_STR);
                $stmt4->execute();
                $bookingID = $stmt4->fetchColumn();
                $stmt4->closeCursor();
                if ( $stmt->execute() ) {
                    $db->commit();
                    $stmt5 = $db->prepare('SELECT b.*, (p.hourly_rate * b.duration) as cost FROM booking b JOIN member m ON (b.memberno = m.memberno) JOIN membershipplan p ON (m.plan = p.title) WHERE b.bookingid=:bookingID');
            $stmt5->bindValue(':bookingID',$bookingID,PDO::PARAM_INT);
                    $stmt5->execute();
                    if ( $stmt->execute() ) {
                        $results = $stmt5->fetch();
                        $stmt5->closeCursor();
                        $results['status']='success';
                        return $results;
                    }else{ //STMT 5 - confirm Booking Insert and Cost - Fail
                        $db->rollback();
                        $results['status']=' Error making booking and getting cost. Check that membership plan is set.';
                        return $results;
                    }
                }else{ //STMT 4 - Insert Booking Data - Fail
                    $db->rollback();
                    $results['status']='Error making booking.';
                    return $results;
                }
            } else { //STMT 3 - Check Car Dimensions - Fail
                $db->rollback();
                $results['status']='Your car is too big for this parking bay.';
                return $results;
            }                
        } else { //STMT 2 - Check Existing Bookings - Fail
            $db->rollback();
            $results['status']='A booking already exists at this time.';
            return $results;
        }                 
    } else { //STMT 1 - Check Parking Availability - Fail
        $db->rollback();
        $results['status']='This parking bay is not available at this time.';
        return $results;
    }
} catch (PDOException $e) {
    print "Error submitting query to databse: " . $e->getMessage();
;
}
}

其中:

function isWeekend($bookingDate) {
return (date('N', strtotime($bookingDate)) >= 6);

1 个答案:

答案 0 :(得分:2)

编辑以了解流程

不要害怕贬票;他们不是个人的。他们在那里是因为观众没有看到自己试图解决问题的证据。

由于您是新的SO用户,因此以下是一些问题排查提示:

作为一项基本规则,要弄清楚错误的位置,请将其细分为基本部分。在您的情况下,请查看驱动该特定错误的原因。在这种情况下,这是因为这一行if ( !empty($result) )。如果不是,则指示它打印该消息;它来自你的else声明。这意味着$result为空。如果它不是空的,你需要找出原因。

在这个特定情况下,您似乎没有在查询中进行任何类型的行计数,因此默认情况下,它会显示为空。

您可以通过执行以下操作来获取行数(使用您的代码示例):

$row_count = $result->rowCount();

来自此处的文档:http://php.net/manual/en/pdostatement.rowcount.php

  

使用PDO :: query()发出SELECT COUNT(*)语句,其语句与预期的SELECT语句相同,然后使用PDOStatement :: fetchColumn()来检索将返回的行数。然后,您的应用程序可以执行正确的操作