I have this code:
$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));
Well how can I use the result of my query? It returns either 1
when all conditions on WHERE
clauses are true or nothing when something's wrong. Well I need to know how can handle the result of a query which starts with SELECT 1
?
You don't even need to know what the result is, since you'll only see a 1
anyway, so all you need to do is see if there were results (and how many): 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
}
When you are selecting just a single row that contains a single value, you have to use fetchColumn().
Note that you cannot use method chaining for SELECT queries with vanila PDO
$stmt = $db->prepare("SELECT 1 whatever");
$stmt->execute(array($id));
$result = $stmt->fetchColumn();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.