| | |
Help with query
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Jan 2008
Posts: 100
Reputation:
Solved Threads: 1
Hey Guys,
I need a little help. I have a calendar script that I want to write a query for and pull out the day of the week, then have it send an email showing all the entries for that day. Unfortunately i'm querying a day, month, year as seperate fields and they are int not date fields. I have accomplished querying the database and used a date/mktime function to convert the int fields into a date field. Where I'm running into trouble is, I can't figure out how to pull this date field and compair it to todays date (date()), and have it display only the records from todays. Here is the code I have so far, I know it is very simple so If you have a better way of doing this, please let me know.
This displays:
2009-01-07 (test, test)
2009-05-19 (test, test)
2009-05-20 (this is a test, today is 20 may)
2009-05-25 (Have a Great Day!!, Memorial Day)
2009-05-25 (test, testing day)
bad if
So far it is doing what I want but I want only those listed above that match today's date. Then I can take those items and send them out in an e-mail.
Any help is much appreciated. Thx.
I need a little help. I have a calendar script that I want to write a query for and pull out the day of the week, then have it send an email showing all the entries for that day. Unfortunately i'm querying a day, month, year as seperate fields and they are int not date fields. I have accomplished querying the database and used a date/mktime function to convert the int fields into a date field. Where I'm running into trouble is, I can't figure out how to pull this date field and compair it to todays date (date()), and have it display only the records from todays. Here is the code I have so far, I know it is very simple so If you have a better way of doing this, please let me know.
PHP Syntax (Toggle Plain Text)
<?php require("config.php"); mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error()); mysql_select_db(DB_NAME) or die(mysql_error()); $query = "SELECT * FROM pec_mssgs ORDER BY m, d"; $result = mysql_query($query) or die('Nope, query didn\'t work'); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $d = $row['d']; $m = $row['m']; $y = $row['y']; $title = $row['title']; $text = $row['text']; $newdate = date("Y-m-d", mktime(0, 0, 0, $m, $d, $y)); echo "$newdate ($text, $title)<br>\n"; } if ($newdate == date("Y-m-d")) { echo "good if"; } else echo "bad if"; ?>
This displays:
2009-01-07 (test, test)
2009-05-19 (test, test)
2009-05-20 (this is a test, today is 20 may)
2009-05-25 (Have a Great Day!!, Memorial Day)
2009-05-25 (test, testing day)
bad if
So far it is doing what I want but I want only those listed above that match today's date. Then I can take those items and send them out in an e-mail.
Any help is much appreciated. Thx.
•
•
Join Date: Jan 2008
Posts: 100
Reputation:
Solved Threads: 1
I tried this:
My result was "Nope, query didn't work". Did I put it wrong in my select statement?
Thanks.
PHP Syntax (Toggle Plain Text)
$todaysMonth = date('m'); $todaysDay = date('d'); $todaysYear = date('y'); mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error()); mysql_select_db(DB_NAME) or die(mysql_error()); $query = "SELECT * FROM pec_mssgs WHERE m=$todaysMonth, d=$todaysDay, y=$todaysYear"; $result = mysql_query($query) or die('Nope, query didn\'t work');
My result was "Nope, query didn't work". Did I put it wrong in my select statement?
Thanks.
It should be
Edit: Ezzaral beat me to it
php Syntax (Toggle Plain Text)
$query = "SELECT * FROM pec_mssgs WHERE m=$todaysMonth AND d=$todaysDay AND y=$todaysYear";
Edit: Ezzaral beat me to it
Last edited by Will Gresham; May 20th, 2009 at 4:11 pm.
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
•
•
Join Date: Jan 2008
Posts: 100
Reputation:
Solved Threads: 1
Ok, all is working well, I put the email functions in and it is working fine. My only problem now is that if there are more than one item on the calendar for that day, the email lists only the last item, not all of them. I need it to either list all of them, or one email for each, either one is acceptable. Here is the code I have so far:
I'm thinking I need some sort of a loop maybe?
PHP Syntax (Toggle Plain Text)
<?php require("config.php"); $todaysMonth = date('m'); $todaysDay = date('d'); $todaysYear = date('Y'); echo "the year is: $todaysYear<br>"; echo "the month is: $todaysMonth<br>"; echo "the day is: $todaysDay<br>"; //echo $today; mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error()); mysql_select_db(DB_NAME) or die(mysql_error()); $query = "SELECT * FROM pec_mssgs WHERE m=$todaysMonth AND d=$todaysDay AND y=$todaysYear"; $result = mysql_query($query) or die('Nope, query didn\'t work'); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $d = $row['d']; $m = $row['m']; $y = $row['y']; $title = $row['title']; $text = $row['text']; $newdate = date("Y-m-d", mktime(0, 0, 0, $m, $d, $y)); $to = "myemail$here.com"; $subject = "Today's Calendar Reminder"; $message = "Today's Calendar Reminder: $title"; $from = "Web Calendar Reminder"; $headers = "From: $from"; echo "$newdate ($text, $title)<br>\n"; } if ($newdate == date("Y-m-d")) { mail($to,$subject,$message,$headers); } else echo "bad if"; ?>
I'm thinking I need some sort of a loop maybe?
When you assign a value to $message, use
EDIT: Also, take some of those statements that will not change out of the WHILE statement (i.e. the $from and $subject) otherwise they are having the same values written to them multiple times.
While will loop through all results.
.= rather than just = , this will append the text to the end of the current value. You may also want to put an \r\n onto the end of the string to add a new line at the end of each event.EDIT: Also, take some of those statements that will not change out of the WHILE statement (i.e. the $from and $subject) otherwise they are having the same values written to them multiple times.
While will loop through all results.
Last edited by Will Gresham; May 20th, 2009 at 5:02 pm.
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
•
•
Join Date: Jan 2008
Posts: 100
Reputation:
Solved Threads: 1
Thank you Xan, I entered this:
it displays multiple events, but does not put a return break after each one. Also, is there a way to put the part before $title only once, otherwise it prints "Today's Calendar Events Reminder" for each event. If it does, its ok for now, but looks funny 
thanks again.
PHP Syntax (Toggle Plain Text)
$message .= "Today's Calendar Events Reminder: $title\r\n";

thanks again.
Rather than
Just put
Then before use mail() put:
php Syntax (Toggle Plain Text)
$message .= "Today's Calendar Events Reminder: $title\r\n";
php Syntax (Toggle Plain Text)
$message .= "$title\r\n";
Then before use mail() put:
php Syntax (Toggle Plain Text)
$message = "Today's Calendar Events Reminder: " . $message;
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
![]() |
Similar Threads
- Sql Query (VB.NET)
- Removing Query Strings (ASP.NET)
- Double MySQL Query (PHP)
- Dynamic Query (JSP)
- MySQL nested query / joined query conversion help (MySQL)
- problem with lengthy query (Java)
- Retreiving variables from a sql query into a form (PHP)
- Query Building (Database Design)
Other Threads in the PHP Forum
- Previous Thread: Concurrent Query ?
- Next Thread: Arrays: Invertated Commas and Apostrophes
| Thread Tools | Search this Thread |
apache api array beginner binary body broken buttons cakephp checkbox class cms code cron curl database date date/time display dynamic ebooks echo email error file files folder form forms function functions global google href htaccess html image include insert ip javascript joomla limit link list login mail mediawiki menu mlm msqli_multi_query multiple mycodeisbad mysql number oop parameter paypal pdf php phpincludeissue phpmyadmin problem query radio random recursion regex remote script search seo server sessions sms source sp space speed sql static subdomain syntax system table tag tutorial update upload url validator variable vbulletin video web webdesign white wordpress xml youtube






