Jump to content

Running a SELECT query inside foreach loop


aliks0905

Recommended Posts

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>";

	?>

Link to comment
Share on other sites

What do you get if you just run this?

 


$filename = "datelist/aprildates.txt";
$fd = fopen ($filename, "r");
$contents = fread ($fd,filesize ($filename));
fclose ($fd); 
$delimiter = ";";
$splitcontents = explode($delimiter, $contents);
foreach ( $splitcontents as $date )
{
  echo $date . "</br>";
}

Link to comment
Share on other sites

Run this and make sure that all of queries are coming out correct.

 

$filename = "datelist/aprildates.txt";
$fd = fopen ($filename, "r");
$contents = fread ($fd,filesize ($filename));
fclose ($fd); 
$delimiter = ";";
$splitcontents = explode($delimiter, $contents);
foreach ( $splitcontents as $date )
{
  echo "SELECT * FROM slotinfo WHERE date = '$date'";
echo "<br />";
}

Link to comment
Share on other sites

The results I receive are:

 

The dates for the $date field are correct except for the very last one with no date.

 

Select a date: SELECT * FROM slotinfo WHERE date = '2010-04-02'
SELECT * FROM slotinfo WHERE date = ' 2010-04-05'
SELECT * FROM slotinfo WHERE date = ' 2010-04-07'
SELECT * FROM slotinfo WHERE date = ' 2010-04-09'
SELECT * FROM slotinfo WHERE date = ' 2010-04-12'
SELECT * FROM slotinfo WHERE date = ' 2010-04-14'
SELECT * FROM slotinfo WHERE date = ' 2010-04-16'
SELECT * FROM slotinfo WHERE date = ' 2010-04-19'
SELECT * FROM slotinfo WHERE date = ' 2010-04-21'
SELECT * FROM slotinfo WHERE date = ' 2010-04-23'
SELECT * FROM slotinfo WHERE date = ' 2010-04-26'
SELECT * FROM slotinfo WHERE date = ' 2010-04-28'
SELECT * FROM slotinfo WHERE date = ' 2010-04-30'
SELECT * FROM slotinfo WHERE date = ''

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.