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

}

Link to comment
Share on other sites

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

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.