473,662 Members | 2,693 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("SE LECT * FROM tbl WHERE id = '1' ");
while($row = mysql_fetch_arr ay($result)) {
foreach ($row as $key =$value) {

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

}

Aug 4 '06 #1
3 3007
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("SE LECT * FROM tbl WHERE id = '1' ");
while($row = mysql_fetch_arr ay($result)) {
foreach ($row as $key =$value) {

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

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

Change it to mysql_fetch_ass oc 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("SE LECT * FROM tbl WHERE id = '1' ");
while($row = mysql_fetch_arr ay($result)) {
foreach ($row as $key =$value) {

echo "<tr><td style='width:15 0px'>$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("SE LECT * FROM tbl WHERE id = '1' ");
while($row = mysql_fetch_arr ay($result)) {
foreach ($row as $key =$value) {

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

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

Change it to mysql_fetch_ass oc 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_ass oc()

Aug 5 '06 #4

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

Similar topics

4
1898
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 the time and the message, and that for each row. Basicly the code would look like this, to give an idea... <?php
10
2311
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* and use it to load an texture from some outside functions. My problem lies in the "for loop", the Code goes as follows. //I create the array of pointers, I'm using 2 just for the sake of an example MapTextures = new unsigned int;
7
5547
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
1175
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 a loop variable that allows me to display a status message such as "Processing 4 out of 10". Is there a more elegant way to code the following and avoid having to declare the index variable? DirectoryInfo diImport = new...
104
7142
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 foeach(Color c in Red..Blue) { } // using enums It should work with all integral datatypes. Maybe we can step a bit further: foeach(int i in 1..10, 30..100) { } // from 1 to 10 and 30 to hundred
3
3286
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
2669
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 "foreach" loop instead of a "for" loop. This made absolutely no sense to me, but the author had posted his code and timing results. The "foreach" (a VB and other languages construct) was 0.01 seconds to access 1000 lines in rich text box,...
7
3804
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: foreach (DataColumn column in view.Row.Table.Columns) { // Do something }
2
3222
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 on the homepage of a site, i wanted to truncate the content to like the first 75 characters and then put "..." (a perfect use of the smarty "truncate" modifier) and then give the visitor a link to read the whole article. But since "the_content" is a...
0
8435
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8547
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6186
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5655
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4181
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4348
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.