Receiving the following error message for this query when I add the arithmetic in the last select ... it is a pretty simple calculation so not sure what the issue is :
alert.last_email is a utc timestamp (unsigned integer)
alerts.email_rate is value in minutes (unsigned integer)
This is a message queue... I am trying to get sends_in
to be a value of seconds in which the message will be sent.
For example using normal time instead of timestamps... last email sent at 7:00pm, rate is 15 minutes, so it would be sent at 7:15. If current time is 7:10 then sends in would be 5 minutes or 300 seconds.
Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 505364672 bytes) in Unknown on line 0
$stmt = $db->prepare("
SELECT
users.username AS username,
computers.computer_name AS computer_name,
alerts.last_email AS last_email,
alerts_queue.alert_id AS alert_id,
alerts_queue.event_title AS event_title,
alerts_queue.event_target AS event_target,
alerts_queue.capture_timestamp AS capture_timestamp,
alerts.last_email + (alerts.email_rate * 60) - UNIX_TIMESTAMP() AS sends_in
FROM computers
INNER JOIN users
ON users.computer_id = computers.computer_id
INNER JOIN alerts
ON alerts.user_id = users.user_id
INNER JOIN alerts_queue
ON alerts_queue.user_id = alerts.user_id
WHERE computers.account_id = :cw_account_id AND computers.status = :cw_status
");
$binding = array(
'cw_account_id' => $_SESSION['user']['account_id'],
'cw_status' => 1
);
$stmt->execute($binding);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
UPDATE :
Calculations are not working as expected :
TIMESTAMPDIFF(SECOND, UTC_TIMESTAMP(), FROM_UNIXTIME(UNIX_TIMESTAMP() + alerts.email_rate * 60)) AS sends_in
Using this as a test... the difference should be 900 seconds, yet it returns -13500.
TIMESTAMPDIFF(SECOND, UTC_TIMESTAMP(), FROM_UNIXTIME(UNIX_TIMESTAMP() + 15 * 60)) AS sends_in
UPDATE :
error on my part - this returns the correct value :
TIMESTAMPDIFF(SECOND, NOW(), FROM_UNIXTIME(UNIX_TIMESTAMP() + 15 * 60)) AS sends_in
WORKING SOLUTION :
It looks a little sloppy, but this works. TIMESTAMPDIFF uses the current timezone... my last_email
value is a unix timestamp which is where the FROM_UNIXTIME comes into play. Because last_email
is an unsigned integer the arithmetic will not work properly unless it is converted to a signed integer. The reason I always set my timestamps as unsigned is because there will never be a negative, but in the case of doing arithmetic with MySQL this caused an issue.
TIMESTAMPDIFF(SECOND, NOW(), FROM_UNIXTIME(CONVERT(alerts.last_email,SIGNED) + alerts.email_rate * 60)) AS sends_in