Jump to content

Query a date column by month only


flashermx

Recommended Posts

I have a MySQL table containing a date column that is formatted as per MySQL
year-month-day

In creating a search form in Dreamweaver 8 I developed a sql query to poulate a drop-down list menu containing all available months refomatted the way I want. See below:

SELECT DISTINCT DATE_FORMAT(sweventcalendar.eventdate, '%M') AS fomattedeventmonth
FROM sweventcalendar
WHERE sweventcalendar.eventdate >= NOW()

However, when I pass the form variable, the selection from the drop-down list, which is called eventmonth, using the GET action, I get no results.

I know my querry works if I remove the date format from the code above and simply populate the drop-down list menu with all distinct dates represented by years-month-day.

My querry results is coded as follows:

SELECT sweventcalendar.eventtitle, sweventcalendar.eventdesc, sweventcalendar.eventurl1, sweventcalendar.eventurl2, sweventcalendar.eventurl3, DATE_FORMAT(sweventcalendar.eventdate, '%M %e, %Y') AS fomattedeventdate
FROM sweventcalendar
WHERE eventdate = colname

/*colname is a date type with a default value of -1 and a run time value of $_GET['eventmonth'] */

I would like query the table by month only.

Any help would be appreciated.

PS: is there something with the way I formated the date in setting up the form element that removes its value?
Link to comment
Share on other sites

If you want to compare a month, then you need to tell the database that. If $_GET['eventmonth'], is numeric (1, 2, 3, ...), then...

[code]... WHERE MONTH ( eventdate ) = intval ( $_GET['eventmonth'] );[/code]

Or if your trying to compare a month name, (May, June, july)

[code]... WHERE MONTHNAME ( eventdate ) = '" . mysql_real_escape_string ( $_GET['eventmonth'] ). "'"; mysql 5.0.25 or ^[/code]

Or you could use....

[code]... WHERE DATE_FORMAT(eventdate, '%M') = '" . mysql_real_escape_string ( $_GET['eventmonth'] ). "'";[/code]

There all kinds of different ways to do this, what way is best, depends on how your using the result, these are just a few basic ways.


printf
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.