blinks Posted April 16, 2011 Share Posted April 16, 2011 The first WHILE loop in the code below is executing correctly on the first go-round, and successfully updating the trade_offers table at the end of the loop. In the 2nd go round, it should use the updated values from trade_offers, but instead is using the original values. Would appreciate any help in locating the problem. There are no errors being output. $getdeclined="SELECT * FROM trade_offers WHERE pokemon_trade_id=$id"; $declined=mysql_query($getdeclined); while($row = mysql_fetch_array($declined)){ $getpostrade="SELECT * FROM trade_offers WHERE pokemon_trainer = '$row[pokemon_trainer]'"; $postrade=mysql_query($getpostrade) or die(mysql_error());; while ($row2 = mysql_fetch_array($postrade)){ $tradepos = $row2['pokemon_pos'];} if ($tradepos==NULL){ $tradepos=0;} $getpostrainer="SELECT * FROM pokemon_trainer WHERE pokemon_trainer = '$row[pokemon_trainer]'"; $postrainer=mysql_query($getpostrainer) or die(mysql_error());; while ($row3 = mysql_fetch_array($postrainer)){ $trainerpos = $row3['pokemon_pos'];} if ($tradepos>$trainerpos){ $newpos = $tradepos;} else if ($trainerpos>$tradepos){ $newpos = $trainerpos;} $newpos +=1; $update="UPDATE trade_offers SET pokemon_pos = $newpos WHERE pokemon_id = $row[pokemon_id]"; $result=mysql_query($update) or die(mysql_error());; $newpos = 0; } Quote Link to comment https://forums.phpfreaks.com/topic/233926-while-loop-using-wrong-value/ Share on other sites More sharing options...
sunfighter Posted April 17, 2011 Share Posted April 17, 2011 To answer your question, your values are gotten from pokemon_trade_id and thats when it equals $id. You never change either value so your loop just repeats. Additional help => most of your queries have a double ; at the end. Don't know if this hurts anything, but it can't be good. Go buy a gun and take the guy that thought you to use 'SELECT *' out and shoot him. Your over taxing your database and it soon will ask for a raise for doing all the unnecessary work or it will go out on strike. Your main query only uses two columns. $row[pokemon_id] and $row[pokemon_trainer] why grab everything? $getdeclined="SELECT pokemon_id, pokemon_trainer FROM trade_offers WHERE pokemon_trade_id = $id"; $declined=mysql_query($getdeclined); while($row = mysql_fetch_array($declined){ $first = $row['pokemon_trainer']; $second = $row['pokemon_id']; ...... You can do this with all your queries. The next one only uses the last row of data: $getpostrainer="SELECT * FROM pokemon_trainer WHERE pokemon_trainer = '$row[pokemon_trainer]'"; $postrainer=mysql_query($getpostrainer) or die(mysql_error());; while ($row3 = mysql_fetch_array($postrainer)) { $trainerpos = $row3['pokemon_pos']; } Your while loop here goes through all rows and ends with the value of $trainerpos set to the value of last row.. Use $getpostrainer="SELECT pokemon_pos FROM pokemon_trainer WHERE pokemon_trainer = '$first' ASC limit 1"; Quote Link to comment https://forums.phpfreaks.com/topic/233926-while-loop-using-wrong-value/#findComment-1202680 Share on other sites More sharing options...
blinks Posted April 17, 2011 Author Share Posted April 17, 2011 Thanks sunfighter, have applied your suggestions, and everything works!! Thank you so much, I have been struggling with this code for almost 2 weeks. Quote Link to comment https://forums.phpfreaks.com/topic/233926-while-loop-using-wrong-value/#findComment-1202721 Share on other sites More sharing options...
sunfighter Posted April 17, 2011 Share Posted April 17, 2011 Thank you for the kinds words. Glad to be of help. Quote Link to comment https://forums.phpfreaks.com/topic/233926-while-loop-using-wrong-value/#findComment-1202750 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.