galvin Posted November 18, 2009 Share Posted November 18, 2009 I have a MySQL table with a bunch of entries, all with their own UNIQUE ID. When I run this code, it's echoing each one TWICE, and I can't figure out why. The output looks like... The id=1002The id=1002The id=1005The id=1005The id=1008The id=1008.... Can anyone tell why it's echoing each one twice? (if this isn't enough info, let me know). And I know the way I'm doing it with a foreach loop is not the best, most efficient way, but I need to do it this way for other code that I haven't written yet... $sql = "SELECT playerid FROM users"; $getids = mysql_query($sql, $connection); if (!$getids) { die("Database query failed: " . mysql_error()); } else { while ($idarray = mysql_fetch_array($getids)) { foreach ($idarray as $value) { echo "The id=" . $value; } } } Quote Link to comment https://forums.phpfreaks.com/topic/181941-solved-echoing-each-id-twice-and-cant-figure-out-why/ Share on other sites More sharing options...
Stuie_b Posted November 18, 2009 Share Posted November 18, 2009 Your doing a loop within a loop.. try placing the foreach loop outside of the while (or just get rid of the while loop) eg $idarray = mysql_fetch_array($getids,MYSQL_ASSOC); foreach($idarray as $value){ echo "id=".$value; } the example above maintains the $idarray while giving the output... not sure if it will work for what every you have planned but it should hopefully be a starting point.. Stuie Quote Link to comment https://forums.phpfreaks.com/topic/181941-solved-echoing-each-id-twice-and-cant-figure-out-why/#findComment-959689 Share on other sites More sharing options...
galvin Posted November 18, 2009 Author Share Posted November 18, 2009 Thanks Stuie. That stopped it from echoing each one TWICE, but now it only echos the FIRST ID, not all of them. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/181941-solved-echoing-each-id-twice-and-cant-figure-out-why/#findComment-959696 Share on other sites More sharing options...
mrMarcus Posted November 18, 2009 Share Posted November 18, 2009 lose the foreach(): while ($idarray = mysql_fetch_array($getids)) { echo 'The id=' . $idarray['playerid'].'<br />'; } will work; Quote Link to comment https://forums.phpfreaks.com/topic/181941-solved-echoing-each-id-twice-and-cant-figure-out-why/#findComment-959702 Share on other sites More sharing options...
galvin Posted November 18, 2009 Author Share Posted November 18, 2009 Thanks Mr. Marcus Quote Link to comment https://forums.phpfreaks.com/topic/181941-solved-echoing-each-id-twice-and-cant-figure-out-why/#findComment-959708 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.