I'm creating a calendar which retrieves a list of dates specified in a file and compares that to the dates in the database that are already scheduled. The problem is I am not very well educated in PHP and MySQL therefore not sure why the following code only returns the correct number of rows for the first date, yet does not for any of the subsequent dates.
As a side note, in this code we're being connected to the database many times, however I've also tried connecting to the database outside the loop - which didn't make any changes.
Any help greatly appreciated!
<?php
//file to retrieve dates
$filename = "datelist/aprildates.txt";
$fd = fopen ($filename, "r");
$contents = fread ($fd,filesize ($filename));
fclose ($fd);
$delimiter = ";";
$splitcontents = explode($delimiter, $contents);
//commented out temporarily - echo "<select name='reservedate'>";
foreach ( $splitcontents as $date )
{
//connect to db
$link = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db ("slots") or die(mysql_error());
$query = mysql_query("SELECT * FROM slotinfo WHERE date = '$date'", $link) or die(mysql_error());
$numrows = mysql_num_rows($query);
//testing to see query results
echo $date." ";
echo $numrows."<br/>";
//if less than 5 dates in db, then echo the option to select that date
if ($numrows > 5) {
} else {
$date2 = date('l, M d, Y', strtotime($date));
//commented out temporarily - echo "<option value='$date'>$date2</option>\n";
}
mysql_close($link);
$link = null;
}
//commented out temporarily - echo "</select>";
?>