php - MySQL query - Anniversary of Date Field

941

I am setting up a mysql database with 2 fields one is a name (text string) and one with a date.yyyy-mm-dd.

I need to query the table to show only the names that match the dd-mm. So on the anniversary of that date every year those names get printed.

$query = "SELECT * FROM FRSLNamelist WHERE $dateofdeath"; 
$result = mysql_query($query); 
while ($line = mysql_fetch_assoc($result)) { 
echo $line['fullname'];
echo "<br>";

I can't find anywhere how to do this. I know that you can explode the date field separated so you only have to match the month and day.

I need the function to do:

If[datefield] = [current.day.month] thenprint [name].

anybody have quick fix.

Thanks in Advance.

499

Answer

Solution:

If the stored date is in formatyyyy-mm-dd as you mentioned, and you have to ignore theyyyy part then one way would be:

$dateOfdeath=date("m-d");
$query = "SELECT * FROM FRSLNamelist WHERE  dateField LIKE '%-$dateofdeath'"; 

Another, even better, way would be:

$d=date("d");
$m=date("m");
$query = "SELECT * FROM FRSLNamelist WHERE MONTH(dateField)=$m AND DAY(dateField)=$d"; 

Edit:

with the dateFieldName, could i use curdate?

Yes

SELECT * FROM FRSLNamelist 
WHERE MONTH(dateField)=MONTH(CURDATE())
AND DAY(dateField)=DAY(CURDATE())

People are also looking for solutions to the problem: php - Propel not adding single quote to fields name in MySql query

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.