# Foreach giving (I think) odd results

**URL:** <https://forum.kirupa.com/t/foreach-giving-i-think-odd-results/291383>\
**Category:** Uncategorized\
**Created:** [June 29, 2009, 12:08am UTC](https://forum.kirupa.com/t/foreach-giving-i-think-odd-results/291383 "2009-06-29T00:08:10Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![W92Baj](https://avatars.discourse-cdn.com/v4/letter/w/a88e4f/32.png) [@W92Baj](https://forum.kirupa.com/u/W92Baj)\
**Post date:** [June 29, 2009, 12:08am UTC](https://forum.kirupa.com/t/foreach-giving-i-think-odd-results/291383/1 "2009-06-29T00:08:10Z")

</div>

Hi guys. It has been a while since I last bothered you all with a stupid question so I think it is time for another:

I am querying a db to get the results for a field and then using a foreach to do something with each result.

I have been through many different iterations and experiments to try and get the expected results but have failed.

I will go through it step by step.

Query:

```auto

$idquery = mysql_query("SELECT id FROM foo WHERE bar=1 ");

```

If I run this through SQL on myPHPadmin I get

| [COLOR=blue]ID [/COLOR] |
| --- |
| [COLOR=darkred]1[/COLOR] |
| [COLOR=darkred]2[/COLOR] |

Which is **exactly** what I want and expect.

Now if I add the following code to the page:

```auto

$idrow = mysql_fetch_array($idquery);
foreach ($idrow as $idkey => $idvalue)
{
echo $idkey." - ".$idvalue;
echo "<br />";
}

```

I get:  
[COLOR=darkred]0 - 1[/COLOR]  
[COLOR=darkred]id - 1[/COLOR]

[COLOR=black]I don’t know what the 0 is (or rather why it is there) and I don’t know where the 2 has gone.[/COLOR]

If I replace the ‘id’ in the original query with a ‘\*’ I get every field twice for only one row. The output is  
[COLOR=darkred]0 - 1[/COLOR]  
[COLOR=darkred]id - 1[/COLOR]  
[COLOR=darkred]1 - bob[/COLOR]  
[COLOR=darkred]name - bob[/COLOR]  
[COLOR=darkred]2 - 36[/COLOR]  
[COLOR=darkred]age - 36[/COLOR] etc etc.

What am I doing wrong? I have been at this all night. It should be really simple and I am sure it is something dumb but I am going mad!
