我正在尝试构建动态 PDO SQL 语句。关于如何做得更好的任何想法?
array(3) { [0]=> string(5) "00000" [1]=> NULL [2]=> NULL } free
当我运行这个时,我不断得到......
所以我的 MySQL PDO 语句似乎不能正常工作!
关于如何解决这个问题的任何想法?
$park = $_POST["park"];
$lecturestyle_id = $_POST["lecturestyle"];
$group_size = $_POST["groupsize"];
$roomstructure_id = $_POST["roomstructure"];
$array = explode(",", $_POST["facilities"]);
// change Mon here
echo '<td class="gridSide">
Mon
';
// build facilities search
for($i = 0; $i < count($array); $i++){
if ($array[$i]!=0) {
$fac .= 'AND facilities_id='.$array[$i].' ';
}
else
$fac .= '';
}
echo '</td>';
for ($i = 1; $i <= 9; $i++)
{
// change mon here
echo '<td class="box" id="mon'.$i.'">';
// dynamically build sql query
$sql =
"
SELECT * FROM ts_room rm
LEFT JOIN ts_roomfacilities rf
ON rm.id = rf.room_id
LEFT join ts_facilities f
ON f.id = rf.facilities_id
LEFT JOIN ts_building b
ON rm.building_id=b.id
WHERE capacity>=".$group_size.' ';
$sql .= $fac;
if($park!="Any") {
$sql .= " AND b.park_id=".$park;
}
if($lecturestyle_id!="Any") {
$sql .= " AND lecturestyle_id=".$lecturestyle_id;
}
if($roomstructure_id!="Any") {
$sql .= " AND roomstructure_id=".$roomstructure_id;
}
$sql .= " AND rm.id NOT IN
(SELECT COUNT(*)
FROM ts_request rq
LEFT JOIN ts_allocation a ON a.request_id = rq.id
WHERE
day_id=1 AND period_id=".$i."
OR a.status IS NOT NULL
AND a.status IN ('Pending','Declined','Failed'))";
$stm = $pdo->prepare( $sql );
$rows = $stm->fetchColumn();
echo $rows.'<br>free</td>';
echo '</td>';
}