delete rows in mysql database if selected time has passed [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I want to delete rows from the database once a set amount of time has passed and the code I have isn't working and I am not sure where I have gone wrong. I plan to change the amount of time to 1 month but it is currently 1 minute for testing purposes.
require 'core/init.php';
$remOld = "DELETE * FROM `ratings` WHERE `ratedate` < NOW() - INTERVAL 1 MINUTE";
if ($remOld = $db->query($remOld)){
echo ('records removed');
}else{
echo ('didnt remove records');
}
Any pointers would be much appreciated.

Remove * from query and close open bracket in your query.
DELETE FROM `ratings` WHERE `ratedate` < (NOW() - INTERVAL 1 MINUTE)

try like this
$remOld = "DELETE FROM `ratings` WHERE `ratedate` < NOW() - INTERVAL '1 MINUTE'";
if ($remOld = $db->query($remOld)){
echo ('records removed');
}else{
echo ('didnt remove records');
}

Related

Get all results of 1 year ago and older datetime field [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 12 months ago.
Improve this question
I want to check if an membership of an user is created 1 year ago or longer.
I have this now:
SELECT * FROM users WHERE date >= NOW() - INTERVAL 1 YEAR
But i get results of 17-03-2021 in the list, when that is not an year ago.
I want to get all results from the Users when created account one year ago
or longer than.
How i can do that? I googled but i cant find an solution.
Thanks in advance.
SELECT * FROM users WHERE date < NOW() - INTERVAL 1 YEAR
Where date smaller then one year ago, you had greater then.

SQL: ORDER BY, AND, WHERE returns boolean error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Trying just to order some ids by the date if their status is not active (aktiv = nei) and if its less then 7 days ago they where created, but i'm getting the "mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given" error, and I can't seem to figure out why.
I'm following the general order of caluses (SELECT,FROM,WHERE,GROUP BY,HAVING,ORDER BY). It works if i remove ORDRE BY or the date selection.
$aktivnei = 'SELECT * FROM `test3` WHERE aktiv LIKE "Nei" AND datex BETWEEN (CURRENT_DATE() - INTERVAL 7 DAY) AND CURRENT_DATE(); ORDER BY datex DESC';
$resultaktivnei = mysqli_query($conn,$aktivnei);
<?php
while ($row = mysqli_fetch_assoc($resultaktivnei)) {
echo '<li class="list-group-item">'.$row['id'].' / '.strftime('%H:%M, %e.%b',strtotime($row['datex'])).'</li>';
};
?>
I have also tried this, but getting the exact same error. I have also tried moving the caluses around but to no success.
$aktivnei = 'SELECT * FROM `test3`';
$aktivnei .= ' WHERE aktiv LIKE "Nei" AND datex BETWEEN (CURRENT_DATE() - INTERVAL 7 DAY) AND CURRENT_DATE();';
$aktivnei .= ' ORDER BY datex DESC';
It's probably dead simple, but right now i'm just scratching my head after trying so many different things.
remove semicolon before order by
'SELECT * FROM `test3` WHERE aktiv LIKE "Nei" AND datex BETWEEN (CURRENT_DATE() - INTERVAL 7 DAY) AND CURRENT_DATE() ORDER BY datex DESC'

How to query data expiration time in mysql php [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
In my Database I have products that i saved with the expiration timestamp in the format eg. 2018-22-08. Please how do I select all products that will expire in less than 4 days.
I have tried
SELECT * FROM table WHERE expiration_timestamp = DATE_ADD(CURDATE(), INTERVAL 4 DAY)
You should use less than operator
SELECT * FROM table WHERE expiration_timestamp < DATE_ADD(CURDATE(), INTERVAL 4 DAY)

PHP Calculation from database not calculating correctly. [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to run a sum from numbers taken from a database. However i can't seem to get it to work.
Does anyone have any ideas as to why i keep getting 0 instead of a percentage? if so could you guide me in the right direction.
PHP:
//SQL Original Goal
$goal = "SELECT * FROM login WHERE username = '$login'";
$goalquery = mysql_query($goal);
$goalarray = mysql_fetch_array($goalquery);
$endgoal = $goalarray['goal'];
//Latest Weight
$latestweightarray = mysql_fetch_array($latestweightq);
$currentweight = $latestweightarray['weight'];
//First Weight Recorded
$firstweightarray = mysql_fetch_array($firstweightq);
$firstweight = $firstweightarray['weight'];
$percent = "100";
$progress = (($firstweight - $currentweight) / ($firstweight - $endgoal)) * $percent;
Print Result:
<?php
echo $progress
?>
This just gives: 0
Rookie Error, i had updated the database twice in one day which conflicted the results somehow, changed one of the dates in the database and this solved the problem

Simple MySQL UPDATE only affects certain rows [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
SOLVED
Rows 1, 4, and 10 have a field with a single quote character in them, so I was SQL injecting myself. More reason to switch to prepared statements!
I have a very simple bulk UPDATE query sent from my PHP form which, oddly enough, only affects certain rows. Rows 1, 4, and 10 doesn't get updated; but all other rows get updated.
Here is the query:
$query = "SELECT * FROM SkillDescriptions";
$result = mysql_query($query);
for ($i=1; $i<=mysql_num_rows($result); $i++){
$skillKey = 'Skill' . (string)$i;
$categoryKey = 'Category' . (string)$i;
$descriptionKey = 'Description' . (string)$i;
$sql="UPDATE SkillDescriptions SET
Skill='$_POST[$skillKey]',
Category='$_POST[$categoryKey]',
Description='$_POST[$descriptionKey]'
WHERE id='$i'
";
$result2=mysql_query($sql);
}
I've got a parallel form that does the exact same PHP form processing as this one but has a different database table, which works properly: so the problem most likely lies in the database table (configuration?) and not the code.
Why do only certain rows get updated, in a seemingly random pattern?
UPDATE
Before:
http://i.stack.imgur.com/2hk2l.png
After: (I appended test to the columns)
http://i.stack.imgur.com/ObMn5.png

Categories