This question already has answers here:
Understanding PDO Prepared Statements and Binding Parameters
(1 answer)
How do I bind an INTERVAL param with PDO?
(1 answer)
Include constant in string without concatenating
(14 answers)
Insert a PHP constant into a string
(2 answers)
Closed 3 years ago.
I understand that you can use CASE to select the value of another column to set the interval :
SELECT CASE some_column
WHEN "SOME_VALUE" THEN date_add(date, INTERVAL value DAY)
WHEN "SOME_OTHER_VALUE" THEN date_add(date, INTERVAL value DAY)
END
AS newDate
FROM table
But how can I pass that number in from my script (and not a value in the database).
For example, I have a constant my PHP script which grabs it's value from an .env file :
define("DOWNLOAD_DAYS", getenv("DOWNLOAD_DAYS")); // say this is 3 for example
And my query currently looks like this :
$sql = 'SELECT * FROM orders WHERE user_id = :user_id
AND status = :status
AND date_added > DATE_SUB(NOW(), INTERVAL 7 DAY)
ORDER BY date_added DESC';
How could I use the DOWNLOAD_DAYS constant to replace the 7 in this query?
I've tried ...
AND date_added > DATE_SUB(NOW(), INTERVAL DOWNLOAD_DAYS DAY)
... Which obviously doesn't work as INTERVAL expects a keyword, not a string or a variable.
And also tried binding the entire command with PDO...
$range = 'DATE_SUB(NOW(), INTERVAL ' . DOWNLOAD_DAYS . ' DAY)';
...
AND date_added > :range
...
$stmt->bindParam(':range', $range, PDO::PARAM_STR);
.. But it's having none of that.
Any suggestions?
You can just build the entire query using the constant:
$sql = 'SELECT * FROM orders WHERE user_id = :user_id
AND status = :status
AND date_added > DATE_SUB(NOW(), INTERVAL ' . DOWNLOAD_DAYS . ' DAY)
ORDER BY date_added DESC';
or you can bind to just the interval value
$sql = 'SELECT * FROM orders WHERE user_id = :user_id
AND status = :status
AND date_added > DATE_SUB(NOW(), INTERVAL :interval DAY)
ORDER BY date_added DESC';
$stmt->bindValue(':interval', DOWNLOAD_DAYS, PDO::PARAM_INT);
Related
I want to check if the user has 5 records in my MySQL database in the last hour. That´s how I am doing it now:
$link = mysqli_query($link, "SELECT * FROM find_points WHERE timestamp > '.time()-3600.' AND user_id = '1' ORDER BY id DESC");
if(mysqli_num_rows($link) >= 5) {
echo 'more than 5 results';
}
It looks like it should work, but it doesn't work...
Please use below query
SELECT * FROM find_points WHERE TIMESTAMPDIFF( hour, timestamp , now() ) > 1 AND user_id = '1' ORDER BY id DESC
You can read in manual about TimestampDiff
It can be used to run difference between 2 dates in various formats.
Please check the Demo on SqlFiddle
It shows how TimestampDiff returns result and you can use the same in WHERE clause.
Update
Based on your comment, that the timestamp is stored as Unix Timestamp, you could use the following query:
SELECT * FROM find_points WHERE TIMESTAMPDIFF( hour, FROM_UNIXTIME(timestamp) , now() ) > 1 AND user_id = '1' ORDER BY id DESC
FROM_UNIXTIME will convert your UNIX Timestamp to DateTime Format. You can then pass this to TIMESTAMPDIFF which will calculate difference and return the number of hours.
Hope this helps.
"SELECT * FROM find_points WHERE timestamp > DATE_ADD(NOW(), INTERVAL 1 HOUR) AND user_id = '1' ORDER BY id DESC"
I would like to query a date field to select all entries that are equal to or greater than current time + 6 hours. The commented first entry works, but it is the second entry that I am battling with. I know this syntax is nowhere near correct.
//$query = "SELECT * FROM `order` WHERE order_status_id = 1 AND date_added >= CURRENT_DATE ORDER BY order_id DESC";
$query = "SELECT * FROM `order` WHERE order_status_id = 1 AND CURRENT_TIME >= date_added +6 hours";
$query = "SELECT * FROM `order` WHERE order_status_id = 1 AND NOW()>= DATE_ADD(date_added,INTERVAL 6 HOUR)";
This query will get your date_added value and add 6 hours to it and after will compare it with the CURRENT_TIME. I would suggest to use NOW() instead of CURRENT_TIME in mysql.
You're probably looking for the DATE_ADD (documentation) function. In your case:
$query = "SELECT * FROM `order` WHERE order_status_id = 1 AND your_date_field >= DATE_ADD(CURRENT_TIME, INTERVAL 6 HOURS)";
would likely do what you need it to.
try this query
$query = "SELECT * FROM `order` WHERE order_status_id = 1 AND CURRENT_TIME >= DATE_ADD(date_added, INTERVAL 6 HOUR)";
if the user in the 5 hours has more calls in the past 10 hours, then he should tell me as an example "true". If not, he should say false to me.
if($aufruf = $pdo->prepare("
SELECT
profil_aufrufe.id,
profil_aufrufe.user_id,
profil_aufrufe.aufrufer_id,
profil_aufrufe.date
FROM
profil_aufrufe
WHERE
profil_aufrufe.user_id = :user_id AND profil_aufrufe.date ..."
))
I thought for a long time, but found no way how I can write the SQL code.
Here is your code
if($aufruf = $pdo->prepare("
SELECT
profil_aufrufe.id,
profil_aufrufe.user_id,
profil_aufrufe.aufrufer_id,
profil_aufrufe.date
FROM
profil_aufrufe
WHERE
profil_aufrufe.user_id = :user_id AND profil_aufrufe.date ..."
))
first of all you can't take a column date in your mysql table its reserved keyword of mysql
date
and why are you using alias whenever you don't join any table, there is no need of alias i simplify it i change column date to cdate
if($aufruf = $pdo->prepare("
SELECT
id,
user_id,
aufrufer_id,
cdate
FROM
profil_aufrufe
WHERE
user_id = :user_id AND
cdate between DATE_SUB(NOW(),INTERVAL 5 HOUR) and DATE_SUB(NOW(), INTERVAL 10 HOUR) "));
I think this will work for you
I have this query that indexes my images and orders them by popularity but I cant make the user to choose the interval cause there's something wrong with the query:
switch($Data['data']){
case 'daily':$QueryDate='=CURDATE()';break;
case 'weekly':$QueryDate=' BETWEEN SUBDATE(CURDATE(), INTERVAL 7 DAYS) AND NOW()';break;
case 'monthly':$QueryDate='>CURDATE() - INTERVAL 31 DAYS';break;
default: Core::redirect('image/browse/daily/1');break;
}
$IMGDB = new Database('images');
$query = "SELECT *, (derived.`likes` * 2 + derived.`views`) as `popularity` from
(SELECT *,
(SELECT COUNT(*) FROM `likes` WHERE `like`=I.id AND `date`".$QueryDate.") AS `likes`,
(SELECT SUM(`views`) FROM `views` WHERE `id`=I.id AND `date`".$QueryDate.") AS `views`
FROM images AS I
) AS derived
where 1 ORDER BY `popularity` DESC ";
Only the daily case works.
Here is the error:
SQL Error (1064): You have an error in your SQL syntax;..... to use near 'DAYS) AND NOW()) AS likes, (SELECT SUM(views) FROM views WHERE id= I.id A
The correct syntax for specifying an interval of days uses the DAY keyword. You've used DAYS in:
BETWEEN SUBDATE(CURDATE(), INTERVAL 7 DAYS) AND NOW()
and:
> CURDATE() - INTERVAL 31 DAYS
I am trying to call data from SQL table that is only 3 days old
My table has a lbs-date column in it and is date format. I have tried the following but get no result from the query at all
$result = mysql_query("SELECT *, DATE_FORMAT(datetime, '%y,%m,%d') FROM lbs_trace_etrack
WHERE lbs_date(datetime) = CURDATE() - INTERVAL 3 DAY
ORDER BY lbs_date DESC")
Is there any other way I can call only the last 3 days of information from the SQL my date format is Y/M/D
SELECT *, DATE_FORMAT(lbs_date, '%y,%m,%d')
FROM lbs_trace_etrack
WHERE lbs_date >= CURDATE() - INTERVAL 3 DAY
ORDER BY lbs_date DESC
check DATE_FORMAT. Its syntax is DATE_FORMAT(<date>,format) . Use like this :
SELECT *, DATE_FORMAT(lbs_date , '%y,%m,%d') FROM lbs_trace_etrack
WHERE lbs_date = CURDATE() - INTERVAL 3 DAY
ORDER BY lbs_date DESC