matvespa Posted August 26, 2011 Share Posted August 26, 2011 Hi. I have the following code to delete a row when the date has passed. $today = date("dmY"); $vLongDate = date("dmY", strtotime($thisDate)); if ($today > $vLongDate) { mysql_query("DELETE FROM events WHERE id < '$id'"); } $today is current date, while $vLongDate is the date that is on my database. Am i doing the if statement correctly?? Quote Link to comment https://forums.phpfreaks.com/topic/245770-auto-delete-database/ Share on other sites More sharing options...
AyKay47 Posted August 26, 2011 Share Posted August 26, 2011 yes Quote Link to comment https://forums.phpfreaks.com/topic/245770-auto-delete-database/#findComment-1262338 Share on other sites More sharing options...
skwap Posted August 26, 2011 Share Posted August 26, 2011 post your full code here... & also explain what trouble you are facing ? Quote Link to comment https://forums.phpfreaks.com/topic/245770-auto-delete-database/#findComment-1262344 Share on other sites More sharing options...
AyKay47 Posted August 26, 2011 Share Posted August 26, 2011 you could also do this condition inside of the query.. are you facing an issue or was this just a general question..? Quote Link to comment https://forums.phpfreaks.com/topic/245770-auto-delete-database/#findComment-1262363 Share on other sites More sharing options...
matvespa Posted August 27, 2011 Author Share Posted August 27, 2011 <?php $query = "SELECT * FROM events ORDER BY date asc"; $result = mysql_query($query); while($row = mysql_fetch_array($result)){ $id = $row["id"]; $title = $row["title"]; $thisDate = $row["date"]; $price = $row["price"]; $ShortDate = date("d/m/Y", strtotime($thisDate)); $LongDate = date("j F Y", strtotime($thisDate)); $today = date("dmY"); $vLongDate = date("dmY", strtotime($thisDate)); <?php $query = "SELECT * FROM events ORDER BY date asc"; $result = mysql_query($query); while($row = mysql_fetch_array($result)){ $id = $row["id"]; $title = $row["title"]; $thisDate = $row["date"]; $price = $row["price"]; $ShortDate = date("d/m/Y", strtotime($thisDate)); $LongDate = date("j F Y", strtotime($thisDate)); $today = date("dmY"); $vLongDate = date("dmY", strtotime($thisDate)); if ($today > $vLongDate) { mysql_query("DELETE FROM events WHERE id < '$id'"); } ?> ?> I want to delete row(s) if the date data stored in my database has passed the current date. What happen here is it seem not to be deleting my database row. I have 2 rows which the date have passed the current date. Quote Link to comment https://forums.phpfreaks.com/topic/245770-auto-delete-database/#findComment-1262539 Share on other sites More sharing options...
matvespa Posted August 27, 2011 Author Share Posted August 27, 2011 Sorry. What happen when i execute the code is only the last row in the database is being displayed while the earlier rows are being deleted. Even when the earlier rows date have NOT passed. Quote Link to comment https://forums.phpfreaks.com/topic/245770-auto-delete-database/#findComment-1262540 Share on other sites More sharing options...
DavidAM Posted August 27, 2011 Share Posted August 27, 2011 When comparing the STRING representation of dates, you have to build the string starting with the largest component: i.e. Year, Month, Day, Hour, Minute, Second. $today = date("dmY"); $vLongDate = date("dmY", strtotime($thisDate)); if ($today > $vLongDate) { Using Day, Month, Year; you end up with: 02092011 (Sep 2, 2011) which (as a string) is LESS than 03011900 (Jan 3, 1900). Using Year, Month, Day; it would be 20110902 and 19000103, which would compare safely as a string. Also, your delete logic is deleting all records with an ID less than the current ID (AND NOT EQUAL TO IT), when you have confirmed the date is LESS. You really should ONLY be deleting the ONE ROW that you have tested. However, don't waste resources and time doing this in PHP. Make sure your "date" column is a DATE or DATETIME datatype, then remove the loop and just use a SINGLE query: DELETE FROM events WHERE `date` < CURDATE() Note: Date is a reserved word in SQL so we have to include it in backticks. I highly recommend you rename that column to avoid confusion and problems. Quote Link to comment https://forums.phpfreaks.com/topic/245770-auto-delete-database/#findComment-1262621 Share on other sites More sharing options...
Pikachu2000 Posted August 27, 2011 Share Posted August 27, 2011 'date' is one of those words that sounds like it should be, but is actually not a reserved word in MySQL. Quote Link to comment https://forums.phpfreaks.com/topic/245770-auto-delete-database/#findComment-1262626 Share on other sites More sharing options...
DavidAM Posted August 27, 2011 Share Posted August 27, 2011 Wow, you learn something new every day! I'd go check, but I always have trouble finding the "reserved words" list in the mySql documentation. How can it not be reserved when it is the name of a datatype? Makes no sense to me. In any case, I would avoid using it as a column name (or table name or database name). Quote Link to comment https://forums.phpfreaks.com/topic/245770-auto-delete-database/#findComment-1262632 Share on other sites More sharing options...
matvespa Posted August 27, 2011 Author Share Posted August 27, 2011 //$today = date("Ymd"); //$vLongDate = date("Ymd", strtotime($thisDate)); mysql_query("DELETE FROM events WHERE 'date' < CURDATE()"); This is my code. However, it doesnt delete anything. Is this how to do it? Without the if statement at all? Or am i missing anything? Quote Link to comment https://forums.phpfreaks.com/topic/245770-auto-delete-database/#findComment-1262672 Share on other sites More sharing options...
DavidAM Posted August 27, 2011 Share Posted August 27, 2011 mysql_query("DELETE FROM events WHERE 'date' < CURDATE()"); If "date" were a reserved word (as I had thought) you need back-ticks, NOT quotes mysql_query("DELETE FROM events WHERE `date` < CURDATE()"); Since "date" is NOT a reserved word, you can do it without mysql_query("DELETE FROM events WHERE date < CURDATE()"); Quote Link to comment https://forums.phpfreaks.com/topic/245770-auto-delete-database/#findComment-1262685 Share on other sites More sharing options...
matvespa Posted August 28, 2011 Author Share Posted August 28, 2011 THANK YOU! SOLVED! Quote Link to comment https://forums.phpfreaks.com/topic/245770-auto-delete-database/#findComment-1262912 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.