Page 1 of 1

Month() + mysql

Posted: Fri May 11, 2007 8:26 pm
by bob_the _builder
Hi,

Im trying to grab all records that match the month from:

$eventid = '2007-05-11';

using query:

Code: Select all

$sql = mysql_query("SELECT id, firstname, lastname, phone, cellphone, email, starttime, finishtime, event FROM calendar WHERE MONTH(starttime) LIKE '".$eventid."%' ORDER BY starttime ASC");

Seems to bring back zero rows when there are 2 rows iin he database matching the 5th month.

Have I got it all wrong?

Thanks

Posted: Fri May 11, 2007 8:31 pm
by volka
MONTH() returns a number between 1 and 12. LIKE is a string comparison function.
Therefore MONTH(starttime) LIKE '".$eventid."%' is like e.g '12' LIKE '2007-05-11%' and that will never match.

try

Code: Select all

MONTH(starttime) = MONTH('$eventid')
instead

Posted: Fri May 11, 2007 9:56 pm
by bob_the _builder
Hi,

Code: Select all

WHERE MONTH(starttime) = '$monthnum' && YEAR(starttime) = '$year'
Works


Thanks