TheChaosFactor Posted August 1, 2010 Share Posted August 1, 2010 So I'm trying to put a user selected date into a mysql query, but it seems like no matter what I do I get 0 results even though I already know there's quite a few. The way I have it now 'echo $query;' puts out the expected results, but I'm guessing it's because I'm trying to match a date to a string. I tried strtotime but I'm not sure if I was doing it right.. I also tried sprintf() just in case, but that doesn't seem to have a date feature. The variable $realdate is just to show how I was attempting to use strtotime, it's not an actualy part of the script at this time... $searchdate = $_GET['searchdate']; //$realdate = strtotime($searchdate) //connects to db dbconnect(); $query="SELECT `name`, `date`, SUM(`amount`) as `total_money`, SUM(`bricks`) as `total_bricks` FROM Aces_Donations WHERE `date`= $searchdate ORDER BY `total_money`"; echo $query; $result=mysql_query($query); //Opens the table echo "<table border = '3' width = 40% bgcolor=#A85045><caption><center>Rankings - ".$searchdate.'</center></caption>'; //Loop to run until eof while($row = mysql_fetch_array($result)){ echo "<tr><td>".$rank."</td> <td>".$row['name']. "</td><td>". $row['total_money']."</td><td>".$row['total_bricks'].'</td></tr>'; $rank++; } echo '</table>'; Quote Link to comment https://forums.phpfreaks.com/topic/209517-how-to-put-dynamic-php-date-into-mysql-query/ Share on other sites More sharing options...
PFMaBiSmAd Posted August 1, 2010 Share Posted August 1, 2010 What data type is your `date` column, a mysql DATE? Edit: Also, what format is the $_GET['searchdate']? Quote Link to comment https://forums.phpfreaks.com/topic/209517-how-to-put-dynamic-php-date-into-mysql-query/#findComment-1093880 Share on other sites More sharing options...
TheChaosFactor Posted August 1, 2010 Author Share Posted August 1, 2010 Sorry, yes it's a mysql date, and the date being passed is in the same format as well when it's passed. Quote Link to comment https://forums.phpfreaks.com/topic/209517-how-to-put-dynamic-php-date-into-mysql-query/#findComment-1093882 Share on other sites More sharing options...
PFMaBiSmAd Posted August 1, 2010 Share Posted August 1, 2010 Literal date values must be enclosed in single-quotes inside the query, otherwise they look like a mathematical expression (2010 - 08 - 01 equals 2001). Quote Link to comment https://forums.phpfreaks.com/topic/209517-how-to-put-dynamic-php-date-into-mysql-query/#findComment-1093901 Share on other sites More sharing options...
TheChaosFactor Posted August 1, 2010 Author Share Posted August 1, 2010 But if I enclose a variable in single quotes won't it print the literal string that is that variable instead of the contents of the variable? Edit: Just checked. It does try to query the variable container instead of the contents when it's placed in single quotes Quote Link to comment https://forums.phpfreaks.com/topic/209517-how-to-put-dynamic-php-date-into-mysql-query/#findComment-1093902 Share on other sites More sharing options...
PFMaBiSmAd Posted August 1, 2010 Share Posted August 1, 2010 It sounds like you changed the double-quotes that enclose the whole query into single-quotes. No one stated to do that. I stated that - Literal date values must be enclosed in single-quotes inside the query. Quote Link to comment https://forums.phpfreaks.com/topic/209517-how-to-put-dynamic-php-date-into-mysql-query/#findComment-1093907 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.