473,508 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why does foreach display each loop twice

//this loop doesnt work as intended, each row displays twice, once with
a number as $key and again with key name as $key!?

Any ideas why?

$result = mysql_query("SELECT * FROM tbl WHERE id = '1' ");
while($row = mysql_fetch_array($result)) {
foreach ($row as $key =$value) {

echo "<tr><td style='width:150px'>$key</td><td>$value</td></tr>";

}

Aug 4 '06 #1
3 2998
monomaniac21 wrote:
//this loop doesnt work as intended, each row displays twice, once with
a number as $key and again with key name as $key!?

Any ideas why?

$result = mysql_query("SELECT * FROM tbl WHERE id = '1' ");
while($row = mysql_fetch_array($result)) {
foreach ($row as $key =$value) {

echo "<tr><td style='width:150px'>$key</td><td>$value</td></tr>";

}
....because that's what mysql_fetch_array does. It returns each entry twice.

Change it to mysql_fetch_assoc and see what happens.

-david-

Aug 4 '06 #2
just hint - try to ude var_dump or print_r to see the real content of
variable ;)
it helped me in this kind of problems many times
--
monomaniac21 wrote:
//this loop doesnt work as intended, each row displays twice, once with
a number as $key and again with key name as $key!?

Any ideas why?

$result = mysql_query("SELECT * FROM tbl WHERE id = '1' ");
while($row = mysql_fetch_array($result)) {
foreach ($row as $key =$value) {

echo "<tr><td style='width:150px'>$key</td><td>$value</td></tr>";

}
Aug 5 '06 #3

David Haynes wrote:
monomaniac21 wrote:
//this loop doesnt work as intended, each row displays twice, once with
a number as $key and again with key name as $key!?

Any ideas why?

$result = mysql_query("SELECT * FROM tbl WHERE id = '1' ");
while($row = mysql_fetch_array($result)) {
foreach ($row as $key =$value) {

echo "<tr><td style='width:150px'>$key</td><td>$value</td></tr>";

}
...because that's what mysql_fetch_array does. It returns each entry twice.

Change it to mysql_fetch_assoc and see what happens.

-david-
To be more specific, it returns the results both as an indexed and
associative entry.
That means you have a numeric key and a string key for each field.
Change the fetch method or use another function, like
mysql_fetch_assoc()

Aug 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1887
by: Marco | last post by:
Hi there ;) Im building a MySQL table for a menssaging system, its a table that has 3 fields de UserID, MTime and Mess. My problem is that i cant find a way that using only a mysql query i get...
10
2295
by: Nick L | last post by:
I'm working on a function which creates a pointers to an array of unsigned ints based off a number read from a file. I then continue to read file names from the file, convert the name to a char*...
7
5541
by: Phil | last post by:
Hi, I read somewhere that the new version (v1.1) has improved the performance of 'foreach' over 'for'. Is that true? I did some measurements and I still think for has an upperhand... ? Phil
2
1163
by: Robert Sentgerath | last post by:
Foreach is a relative handy construct to avoid having to create the classic "for(int loop = 0; loop < collection.Length; loop++) construct. Though when I use foreach instead I do not have access to...
104
7046
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
3
3271
by: cody | last post by:
why foreach does always have to declare a new variable? I have to write foreach (int n in array){} but Iam not allowed to write: int n=0; foreach (n in array){}
15
2655
by: Mike Lansdaal | last post by:
I came across a reference on a web site (http://www.personalmicrocosms.com/html/dotnettips.html#richtextbox_lines ) that said to speed up access to a rich text box's lines that you needed to use a...
7
3795
by: David Veeneman | last post by:
This is a very simple question, but for moe reason I can't find an answer. Do I take a performance hit if a foreach statement has to evaluate an expression? For example, consider the following:...
2
3216
by: recordlovelife | last post by:
So I am trying to display a title, date, and content of a wordpress blog. Word press provides nice drop in functions to get the job done with simple names like "the_title", and the "the_content" But...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3194
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.