Skip to content
Advertisement

@SET (SQL) in PHP

How to state SET @unix_four_weeks_ago = UNIX_TIMESTAMP(curdate()) – 2419200; in php file.

SQL

SET @unix_four_weeks_ago = UNIX_TIMESTAMP(curdate()) - 2419200;
SELECT id FROM USERS WHERE timemodified >= @unix_four_weeks_ago;

PHP

$result=$mysqli->query("SET @unix_four_weeks_ago = UNIX_TIMESTAMP(curdate()) -  2419200;SELECT id FROM USERS WHERE timemodified >= @unix_four_weeks_ago; ");

The query is all about getting the last 4weeks records. The query was working fine in SQL, but not giving any output when I run php file.

Advertisement

Answer

As the others already said, multi_query will allow you to run multiple statements in one go. However, this query is quite convoluted and hard to read, you at least have to document the meaning of magic number 2419200. Also, due to DST considerations, this query is not exactly correct for those time bands that contain a DST switchover event. You could also transform this query into

SELECT id from users where timemodified >= UNIX_TIMESTAMP(SUBDATE(curdate(), 28))

which is a bit more readable, and as proper datetime calculations are used, more correct.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement