将此 SQL 语句编写为 PDO 查询的最佳方法是什么?我希望它以数字形式返回查询数。
SELECT COUNT(*) totalCount
FROM ts_room a
INNER JOIN ts_roompref b
ON a.id = b.room_ID
INNER JOIN ts_request c
ON b.request_ID = c.roompref_ID
WHERE c.day_ID = 1 AND c.period_ID = 1
这就是我的想法:
$room_ID = $_POST["room"];
$sql = "SELECT COUNT(*) totalCount
FROM ts_room a
INNER JOIN ts_roompref b
ON a.id = b.:room_ID
INNER JOIN ts_request c
ON b.request_ID = c.roompref_ID
WHERE c.day_ID = 1 AND c.period_ID = 1";
$stm = $pdo->prepare( $sql );
$stm->execute( array( ':room_ID' => $room_ID) );
$rows = $stm->fetchAll();
// echo row count here
$count = $stm->rowCount();