Jump to content

while (mysql_fetch_array) within for loop


canty

Recommended Posts

This is a pain. It seems totally logical to me and I had it working before I accidentally saved over the working version (major boo boo).

 

This is two code snippets, first one is how I want it to work - One Query and the loop through results.

Second does work, but then its going to have to run mysql 60 times, which could get slow as the site gets more use.

 

$results = mysql_query($query);
$response .= "<table><tr>";
if($nit == 0){
for($i = 0; $i < 60; $i++){
	$dateCompare = date("Y-m-d", mktime(0,0,0,date("m"),date("d")+$i,date("Y")));
	$dateForString = date("D d/m/Y", mktime(0,0,0,date("m"),date("d")+$i,date("Y")));
	$response .= "<td height='350px' style='color:white; background-color:grey;'>$dateForString</td>";
	while($row = mysql_fetch_array($results)){
		$id = $row['id'];
		$poster = $row['poster'];
		$date = $row['date'];
		$response .= "$date : $dateCompare<br>";
		if($date == $dateCompare){
			$response .= "<td><a href='fullinfo.php?selected_id=$id'><img src='$poster' height='350px' /></a></td>";
		}
	}
}
}

 

FOLLOWED BY THE SECOND

 

$response .= "<table><tr>";
if($nit == 0){
for($i = 0; $i < 60; $i++){
	$dateCompare = date("Y-m-d", mktime(0,0,0,date("m"),date("d")+$i,date("Y")));
	$dateForString = date("D d/m/Y", mktime(0,0,0,date("m"),date("d")+$i,date("Y")));
	$response .= "<td height='350px' style='color:white; background-color:grey;'>$dateForString</td>";
	$results = mysql_query($query);
	while($row = mysql_fetch_array($results)){
		$id = $row['id'];
		$poster = $row['poster'];
		$date = $row['date'];
		if($date == $dateCompare){
			$response .= "<td><a href='fullinfo.php?selected_id=$id'><img src='$poster' height='350px' /></a></td>";
		}
	}
}
}

 

First one results in -it seems - to only go through the while loop once, the first time. So out put is todays date, for the number of results from the database.

The second works fine, prints out in order and matches up. I just don't like its inefficiency.

 

Thanks

Canty

Link to comment
Share on other sites

I can't really see what you're doing to well enough to optimize it, but in the current configuration of the first example you need to put mysql_data_seek($results, 0)  either right before or right after the while loop.

 

Once you fetch all records from a result the pointer is at the end so you have to reset the pointer to the beginning.

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.