Jump to content

[SOLVED] Query. Trying to select all rows less than 1 day old using date field


hellonoko

Recommended Posts

I am trying to write a query that selects all rows entered less than a day ago by comparing the field that states when the row was created against the current date/time minus 1 day.

 

What is the proper way to do this as my query is failing.

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home2/sharingi/public_html/scrape/display/display.php on line 167

 

<?php


	$current_time = strtotime("now");

	//take one day in seconds from the current time to find the timestamp for 24 hours ago
	$day_old = $current_time - 86400;			

	//change timetamp to a date format to compare
	$day_old = date("Y-m-d H:i:s", $day_old);

	echo $day_old;
	// select all entries that are less than the 1 day old
	$query = mysql_query("SELECT * FROM `mp3links` WHERE `foundtime` < $day_old AND `scraped` != '0' ORDER BY `foundtime` DESC");

?>

 

 

Query fails for some reason. Try displaying mysql error message

$query = mysql_query("SELECT * FROM `mp3links` WHERE `foundtime` < $day_old AND `scraped` != '0' ORDER BY `foundtime` DESC") or die(mysql_error());

to see why

Need single quotes around date in sql query

 


$query = mysql_query("SELECT * FROM `mp3links` WHERE `foundtime` < '".$day_old."' AND `scraped` != '0' ORDER BY `foundtime` DESC") or die(mysql_error());

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.