How to Query Multiple MySQL Databases With PHP Based on the Week Day

While building a new website tonight, I decided that I wanted to present a different message to my visitors, based on the day of the week. Easy enough… using the PHP switch function, which is typically used in replace of multiple if or elseif statements. But then, I decided to take it a step further and present some information from a MySQL database, with the message. Pulling MySQL data with PHP is also a fairly easy task but since I was actually pulling the data from multiple databases, I had to experiment a bit and ultimately used the switch function for this as well.

If you’re trying something similar, feel free to start with the following code I created. It suited my needs perfectly.

<?php
$today=date("w");
switch ($today)
{
case 0:
print("Happy Sunday! <br>Visit this ad in the ‘Directory 1’ section of our website.");
$dbname="database1";
$host="localhost";
$user="database1";
$pass="password";
break;
case 1:
print("Happy Monday! <br>Visit this ad in the ‘Directory 2’ section of our website.");
$dbname="database2";
$host="localhost";
$user="database2";
$pass="password";
break;
case 2:
print("Happy Tuesday! <br>Visit this ad in the ‘Directory 3’ section of our website.");
$dbname="database3";
$host="localhost";
$user="database3";
$pass="password";
break;
case 3:
print("Happy Wednesday! <br>Visit this ad in the ‘Directory 4’ section of our website.");
$dbname="database4";
$host="localhost";
$user="database4";
$pass="password";
break;
case 4:
print("Happy Thursday! <br>Visit this ad in the ‘Directory 2’ section of our website.");
$dbname="database2";
$host="localhost";
$user="database2";
$pass="password";
break;
case 5:
print("Happy Friday! <br>Visit this ad in the ‘Directory 3’ section of our website.");
$dbname="database3";
$host="localhost";
$user="database3";
$pass="password";
break;
case 6:
print("Happy Saturday! <br>Visit this ad in the ‘Directory 4’ section of our website.");
$dbname="database4";
$host="localhost";
$user="database4";
$pass="password";
break;
}
$link = mysql_connect($hostname, $user, $pass);
mysql_select_db($dbname, $link);
//I’m pulling the five newest mysql rows from my database with the next line.
$sql_statement = ‘SELECT title, description FROM databasetable ORDER BY date DESC LIMIT 0 , 5’;
$result = mysql_query($sql_statement, $link);
while ($curr_row = mysql_fetch_assoc($result)) {
$title = $curr_row[‘title’];
$description = $curr_row[‘description’];
//I’m limiting the contents of what I’m pulling to 300 characters. This gives me more control over formatting the page.
$description = substr($description,0,300);
echo ‘<font size="2" face="sans-serif"><h5>’ . $title . ‘</h5>’ . ‘&nbsp;&nbsp;’ . $description . ‘…</font>’;
}
mysql_free_result($result);
?>

Submit a comment

CommentLuv badge

reverse phone lookupTattoo DesignsSEO
My New Amazon Script Is Close!
A new php script for sale is coming!
Sign up to get notified

..