Forum Moderators: open
joindateis not of type DATE, then I would ALTER the definition and make it of type DATE. Then your query should be quite easy using your relational database temporal (date/time) operations. If that is not an option, you could use SUBSTRING to derive that portion of your format which is the month and use that in your WHERE clause comparison.
bluebelldolphin, you said that the
joindateformat is date(d/m/Y). Typically, a stored date is in ISO format,
yyyy-mm-dd. So, first things first -- you probably have not created your
joindatecolumn as column type DATE. You can find out by running a MySQL command that provides information about the columns in a table:
DESCRIBE user;
dd/mm/yyyythen you are going to get NULL values every time you try to use the MySQL built-in DATE functions. This is where I said you would need to use a SUBSTRING against your stored date string value:
$sql1 = "SELECT * FROM user WHERE SUBSTRING(joindate FROM 4 FOR 2) = '".$month."'";
If it is of type DATE, then your query will work as long as your $month variable contains the value 7, which represents the month of JULY (note: you would not need to have the quotation marks around your variable value as the returned value of a MONTH() function is an integer in the range 0 to 12).
$sql1 = 'SELECT * FROM user WHERE MONTH(joindate) = ' . $month;
The latter example is more often used and is the preferred method.