Jump to content

[SOLVED] Echoing each id twice and can't figure out why


galvin

Recommended Posts

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

}

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.