MySQL Query - select all records with this month and year

Posted on 2008-10-28
Last Modified: 2013-12-13

I'm trying to make a mysql query that will take $month and $year variables and select all record in a MySQL database whose 'date' field is "$year-$month-*"

How would I do this?
Question by:Mr_Splash
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 82

Accepted Solution

hielo earned 300 total points
ID: 22822143
mysql_query("SELECT * FROM Table WHERE DATE_FORMAT(`date`,'%Y-%m') = '$year-$m'") or die(mysql_error());

Expert Comment

ID: 22822175
mysql_query("select * from Table where (Month(DateField) = ".$month.") and (Year(DateField) = ".$year.")");
LVL 82

Expert Comment

ID: 22822497
glad to help

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with mod_substitute 18 44
What is this? I've never seen this... 2 29
Length of for loop to be dynamic 2 33
Convert php array to comma seperated list 19 35
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question