Last updated on November 6th, 2024 at 12:27 pm
This document presents an updated tutorial on developing a birthday script that automatically dispatches wishes on the designated day, utilizing PHP and MySQL. To enhance the interactivity of this tutorial, I will establish a sample database featuring a straightforward table that holds users’ birthday information.
I have already created a database named “bday” and will now proceed to add a table and populate it with relevant data.
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)
mysql>
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
<?php
$date = date("m-d");//here my date format in my DB is 12/29, basically it grabs the current date
$today=date("Y-m-d");
$link = mysqli_connect('localhost','YOUR_USER','YOUR_PASSWORD','bday');
if($link)
{
$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";
}
mysqli_close($link);
} ?>
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.
$today=date("Y-m-d");
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 🙂
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
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. 🙂
Hi!
Can you email this script the link seen to be broken
Simple birthday script using php and mysql
Regards
Hi Manjeet,
The link has been fixed. Thanks for bringing this to my attention.
anyone can share a sample script of this? i am really new to php