Jump to content

Loops wiht mysql_fetch_array


ameliabob
 Share

Recommended Posts

I am using the following code but only get to go through the loop one time

		$sql = "SELECT symbol,secondDate,secondPrice,secondRank FROM datafile WHERE secondRank < '9' AND secondDate < '".$testDate."'";		$result = mysql_query($sql)  or die("Six Month Selection didn't work: ".$sql."<br />".mysql_error());echo ("Second rank tests found ".mysql_num_rows($result));							while( $r = mysql_fetch_array($result)) {	echo ( "  ".$r['symbol']);							$update = "UPDATE datafile SET baseRank='".$r['secondRank']."', basePrice='".$r['secondPrice']."', baseDate='".$r['secondDate']."', ";			$update .= "firstRank='9', firstPrice='0.00', firstDate='2000-01-01', ";			$update .= "secondRank='9', secondPrice='0.00', secondDate='2000-01-01' WHERE symbol= '".$r['symbol']."'";			$result = mysql_query($update)  or die("Six month update second date didn't work: ".$update."<br />".mysql_error());		}

Does the fact that I am updating the file that the original query referred to have anything to do with this?

Link to comment
Share on other sites

Does the fact that I am updating the file that the original query referred to have anything to do with this?
By file, do you mean the database? If so, then no. Changing the database has nothing to do with executing code based on data you've already retrieved. If you were making subsequent queries inside the loop to retrieve new data, then it might affect it.The reason it terminates has to do with this line:$result = mysql_query($update) or die("Six month update second date didn't work: ".$update."<br />".mysql_error());You overwrite the $result variable which, prior to starting the loop, held your result set (data). Inside the loop you set it to the return value of the update query, which I think is true or false depending on whether the query was successful. Try renaming that variable. Something like:$updateResult = mysql_query($update) or die("Six month update second date didn't work: ".$update."<br />".mysql_error());
Link to comment
Share on other sites

By file, do you mean the database? If so, then no. Changing the database has nothing to do with executing code based on data you've already retrieved. If you were making subsequent queries inside the loop to retrieve new data, then it might affect it.The reason it terminates has to do with this line:$result = mysql_query($update) or die("Six month update second date didn't work: ".$update."<br />".mysql_error());You overwrite the $result variable which, prior to starting the loop, held your result set (data). Inside the loop you set it to the return value of the update query, which I think is true or false depending on whether the query was successful. Try renaming that variable. Something like:$updateResult = mysql_query($update) or die("Six month update second date didn't work: ".$update."<br />".mysql_error());
Thanks. Its amazing what a fresh pair of eyes sees.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...