我有这段代码:
$db
->prepare("SELECT 1
FROM ( SELECT count(*) AS num_week,
sum(date_time > unix_timestamp(DATE_SUB(now(), INTERVAL 1 day))) as num_day,
sum(date_time > unix_timestamp(DATE_SUB(now(), INTERVAL 1 hour))) as num_hour,
sum(date_time > unix_timestamp(DATE_SUB(now(), INTERVAL 1 minute))) as num_1min
FROM resend_pass
WHERE user_id = ?
AND date_time > unix_timestamp(DATE_SUB(now(), INTERVAL 1 WEEK))
) a
WHERE num_week < 12 AND num_day < 6 AND num_hour < 4 AND num_1min < 1;")
->execute(array($id));
那我怎么能用我的查询结果呢?当1
子句中的所有条件都为真时,它会返回WHERE
,如果出现错误,则返回任何内容。好吧,我需要知道如何处理以SELECT 1
开头的查询结果?
答案 0 :(得分:0)
您甚至不需要知道结果 ,因为您无论如何只会看到1
,所以您需要做的就是查看是否有结果(以及有多少):PDOStatement::rowCount
$stmt = $db
->prepare("SELECT 1
FROM ( SELECT count(*) AS num_week,
sum(date_time > unix_timestamp(DATE_SUB(now(), INTERVAL 1 day))) as num_day,
sum(date_time > unix_timestamp(DATE_SUB(now(), INTERVAL 1 hour))) as num_hour,
sum(date_time > unix_timestamp(DATE_SUB(now(), INTERVAL 1 minute))) as num_1min
FROM resend_pass
WHERE user_id = ?
AND date_time > unix_timestamp(DATE_SUB(now(), INTERVAL 1 WEEK))
) a
WHERE num_week < 12 AND num_day < 6 AND num_day < 4 AND num_1min < 1;");
$stmt->execute(array($id));
if($stmt->rowCount() > 0) {
// Your code
}
答案 1 :(得分:0)
如果只选择包含单个值的单行,则必须使用fetchColumn()。
请注意,您不能对使用vanila PDO的SELECT查询使用方法链接
$stmt = $db->prepare("SELECT 1 whatever");
$stmt->execute(array($id));
$result = $stmt->fetchColumn();