Last updated on December 29th, 2022 at 09:17 am

This is a new updated tutorial on how to create a birthday script that automatically sends wishes to on that particular day using PHP and MySQL.

In order to make this tutorial more interactive I am going to create a sample database with a simple table that contains birthday details of users. I have created a database named bday, and now going to add a table and insert some data to it.

mysql> CREATE TABLE bday_table (
    ->     Personid int NOT NULL AUTO_INCREMENT,
    ->     Name varchar(255) NOT NULL,
    ->     email varchar(255) NOT NULL,
    ->     bday_check DATE NOT NULL,
    ->     PRIMARY KEY (Personid)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO bday_table (Name,email,bday_check) VALUES ('Alan','alan@something','2003-07-21');
Query OK, 1 row affected (0.01 sec)

mysql> INSERT INTO bday_table (Name,email,bday_check) VALUES ('Scott','scot@something','2001-08-14');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO bday_table (Name,email,bday_check) VALUES ('Jen','jen@something','2000-12-29');
Query OK, 1 row affected (0.01 sec)

mysql>  INSERT INTO bday_table (Name,email,bday_check) VALUES ('Mack','mack@something','1997-12-29');
Query OK, 1 row affected (0.00 sec)

mysql> select * from bday_table;
| Personid | Name  | email          | bday_check |
|        1 | Alan  | alan@something | 2003-07-21 |
|        2 | Scott | scot@something | 2001-08-14 |
|        3 | Jen   | jen@something  | 2000-12-29 |
|        4 | Mack  | mack@something | 1997-12-29 |
4 rows in set (0.00 sec)

As you can see the table name is bday_table and one of the column which has the birthday is bday_check (In date format).

Now that we have the database ready, let us jump on the the PHP script

$date = date("m-d");//here my date format in my DB is 12/29, basically it grabs the current date 
$link = mysqli_connect('localhost','YOUR_USER','YOUR_PASSWORD','bday');
    $grabBday = "SELECT * FROM bday_table WHERE bday_check LIKE '%-".$date."'";
    //here it will take the name of the person whose bday is on a particular date
    $result = mysqli_query($link,$grabBday);
if (mysqli_num_rows($result) > 0) {
  // output data of each row
  while($row = mysqli_fetch_assoc($result)) {
    echo $row["Name"]."'s Birthday Is Today ".$row["bday_check"]." An email will be sent to ".$row["email"].".<br>";
    mail($row["email"], 'HAPPY BIRTHDAY', 'MESSAGE');
} else {
  echo "0 results";


}  ?>

How the script works ?

  • Step 1 – Get current date / year
  • Step 2 – Connect to the database and select rows from table that has matching month-date (Make sure to update connection details / table and row names accordingly)
    As you can see we are using SELECT statement with LIKE to get the month/day from the table
$grabBday = "SELECT * FROM bday_table WHERE bday_check LIKE '%-".$date."'";
  • Step 3 – Fetch the rows and loop through each of the matching rows, grab the email from the table send it using MAIL()
  • Step 4 – Close the connection

Tips to run the script

  • You can run this script in your index page or any other pages
  • Call the birthday script accordingly from your main page to verify when the user birthday is etc.,
  • If you are planning to run the script as a cron make sure that the script is executed only once in a day, you need to configure it accordingly.

In this script we are using the simple mail() function. But you can refer these tutorials if you would like to send

I have simply added another variable as shown, just in case if you need to add this to any of your output.


To make the email workflow more beautiful you can use our example script of sending email with attachments using PHP

Note: Due to a lot of demand for this script, I have updated it with more features/details to assist my visitors get things done easily 🙂

10 thoughts on “Simple birthday script using php and mysql”
  1. This information is very usefull, but i’m wonderring how do i install it i mean, do I just place it in my folder name Style or something? and do i call the script or should i run a cron job, how will it take effect? can you please advised me on this. That will be so great. Anticipating thanks

    1. Hello, Thank you so much for your comment. You can run this script either as in a CRON as you said or even when an admin/user try to hit some link, for instance some link named TODAY’s B-DAY and direct the link to a php page that contains the above script. You can call the script by any one of these methods, but ultimate decision on where to use to script is yours. 🙂

  2. I keep getting the following error message on line 8:

    PHP Parse error: syntax error, unexpected ‘    if’ (T_STRING)

    Here is line 8:

    if($rs = mysqli_query($link, $grabBday))

    Can’t figure out what the problem is

    1. Hey Andrew, thanks for taking time sharing with me the error details. Looks to me that there was some issue with the main script. Apologies for that. I have updated the script today with more database related changes and added extra conditions in PHP script to make sure that it works 🙂 . Tested it and works fine for me. Use the updated script and let me know if you still face errors.

  3. Thanks for the updated script. What would I need to change to send the birthday notification to the admin (me and two others) only.

    1. Hello,

      There are multiple ways to do this but one easy method I can think of is to add your email and the other two member email (separated by comma) to a variable and modify the mail command with that variable in the mail() to field instead of $row[“email”]

      The code will look similar to this

      $to = 'adminemail@something,otheremail1@something,otheremail2@something'; //Add this line before the while loop probably just after the $today variable
      mail($to, 'HAPPY BIRTHDAY', 'MESSAGE');//Instead of the $row['email'] which basically send to the birthday person, replace that with $to

      Try this and let me know how it goes. Thanks!!

      Refer PHP Public Document for more insights around the usage of mail().

  4. Got it working great and I even added a From email in a $headers variable. Thanks for your help.

    Few more questions…At the moment, if two people have the same birthday, the script generates two separate email notifications. How can I change this.

    Next question…If I wanted to send an email notification advising the admin of today’s birthday and tomorrow’s birthday, what part of the code needs to be changed. Perhaps it is not possible?

    Thanks for your help on this.

Leave a Reply

Your email address will not be published. Required fields are marked *