jpipes Posted January 14, 2008 Share Posted January 14, 2008 Hello, I'm trying to put together a relatively simply query but I'm having trouble finding a solution that works. I have a table called Calendar with 5 fields in it - ID, Month, Date, Year, and Event. I'd like to run a script that will query this table and based on the current actual date, will pull out those entries that match the same Month and Date entry in the table. If today is January 13 the result will display all entries under Month = January and Date = 13 from within the table. Does anyone have a suggestion or a possible bit of code I could use to accomplish this? Thanks in advance to anyone with any help! Quote Link to comment https://forums.phpfreaks.com/topic/85885-need-help-with-php-mysql-date-related-query/ Share on other sites More sharing options...
marcus Posted January 14, 2008 Share Posted January 14, 2008 <?php $day = date("j"); // 1-31 $month = date("n"); // 1-12 $year = date("Y"); $sql = "SELECT * FROM `calendar` WHERE `day`='".$day."' AND `month`='".$month."' AND `year`='".$year."'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0){ echo "Today is a boring day, nothing is happening!"; }else { echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n"; while($row = mysql_fetch_assoc($res)){ echo "<tr><td>".$row['event']."</td></tr>\n"; } echo "</table>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/85885-need-help-with-php-mysql-date-related-query/#findComment-438491 Share on other sites More sharing options...
jpipes Posted January 14, 2008 Author Share Posted January 14, 2008 Wow! That was very fast! Thanks for the quick response. I will try this script right now and let you know what happens. Quote Link to comment https://forums.phpfreaks.com/topic/85885-need-help-with-php-mysql-date-related-query/#findComment-438514 Share on other sites More sharing options...
jpipes Posted January 14, 2008 Author Share Posted January 14, 2008 I tried it and didn't get any errors but I also didn't get any data either. My database has the month stored as "January" or "March" versus 1-12, if that matters. Again, the fields in the table called Calendar are ID, Month, Date, Year, and Event. Some more background, I'm trying to create a "on this day in history" sort of script where based on todays date (January 13 for example) anything in the database that occured on January 13 will appear. Quote Link to comment https://forums.phpfreaks.com/topic/85885-need-help-with-php-mysql-date-related-query/#findComment-438516 Share on other sites More sharing options...
AndyB Posted January 14, 2008 Share Posted January 14, 2008 I tried it and didn't get any errors but I also didn't get any data either. My database has the month stored as "January" or "March" versus 1-12, if that matters. Again, the fields in the table called Calendar are ID, Month, Date, Year, and Event. It sure does. Modify that script to use the same names as your database table uses. Modify the date determination lines to get the day, month, year in the same format as your database table uses. Refer to the date() function in the manual to see just how to do that. http://ca.php.net/manual/en/function.date.php Quote Link to comment https://forums.phpfreaks.com/topic/85885-need-help-with-php-mysql-date-related-query/#findComment-438525 Share on other sites More sharing options...
jpipes Posted January 14, 2008 Author Share Posted January 14, 2008 That did it! Thanks. I changed date("n") to date("F") and it works! Thank you!! Now I just need customize the output and whatnot. I spent a better part of the afternoon working on this and got a working answer in 15 min or so after posting here. I'm stunned. Thank you. Side question - is it possible to run a php script like this on a page not called xyz.php? Quote Link to comment https://forums.phpfreaks.com/topic/85885-need-help-with-php-mysql-date-related-query/#findComment-438541 Share on other sites More sharing options...
pocobueno1388 Posted January 14, 2008 Share Posted January 14, 2008 is it possible to run a php script like this on a page not called xyz.php? The file can be named whatever it wants as long as it has a .php extension. Quote Link to comment https://forums.phpfreaks.com/topic/85885-need-help-with-php-mysql-date-related-query/#findComment-438543 Share on other sites More sharing options...
jpipes Posted January 14, 2008 Author Share Posted January 14, 2008 That I know, but I am interested in parsing the above php script into an html document. I found out that by adding RemoveHandler .html .htm AddType application/x-httpd-php .php .htm .html to the .htaccess file, php scripts will work ok on html pages without having to rename or call them xyz.php! Very cool! Quote Link to comment https://forums.phpfreaks.com/topic/85885-need-help-with-php-mysql-date-related-query/#findComment-438552 Share on other sites More sharing options...
jpipes Posted January 14, 2008 Author Share Posted January 14, 2008 Just a quick update... the script is working great but after I added the above info to my .htaccess file although php worked embedded in html pages, some of my other scripts (.pl files specifically) stopped working... Anyone know of a "safe" way to parse or embed php into an .html document? Quote Link to comment https://forums.phpfreaks.com/topic/85885-need-help-with-php-mysql-date-related-query/#findComment-438558 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.