Solved

foreach() loop giving double output?

Posted on 2004-08-19
4
237 Views
Last Modified: 2013-12-12
When I run this script:

*******************
$sql = "SELECT * FROM table";
mysql_connect( $Host, $User, $Password )
      or die ( ' Unable to connect to server.' );
mysql_select_db( $DBName )
      or die ( 'Unable to select database.' );
$result = mysql_query($sql)
      or die ( 'Unable to execute query.' );
while ($row=mysql_fetch_array($result)) {
      foreach ($row as $key => $value) {
            print "Key: $key --> Value: $value<br>";
            }
      }
*******************

...I get double the output that I am expecting. For instance, instead of this:

Key: cell1_name --> Value: cell1_value
Key: cell2_name --> Value: cell2_value
Key: cell3_name --> Value: cell3_value

...I get this:

Key: 0 --> Value: cell1_value
Key: cell1_name --> Value: cell1_value
Key: 1 --> Value: cell2_value
Key: cell2_name --> Value: cell2_value
Key: 2 --> Value: cell3_value
Key: cell3_name --> Value: cell3_value

Why? Is this expected behaviour? How do I make it so it only returns the 'single' output?
0
Comment
Question by:TunaMaxx
  • 3
4 Comments
 
LVL 32

Expert Comment

by:ldbkutty
ID: 11846819
>> while ($row=mysql_fetch_array($result)) {

Change to :

while ($row=mysql_fetch_assoc($result)) {
0
 
LVL 32

Accepted Solution

by:
ldbkutty earned 250 total points
ID: 11846839
2nd Method:

>> while ($row=mysql_fetch_array($result)) {

Change it to:

while ($row=mysql_fetch_array($result, MYSQL_ASSOC)) {
0
 

Author Comment

by:TunaMaxx
ID: 11846859
Thanks!

I was just coming back to say I figured out your second suggestion on my own... but your first seems to be tidier. Is the an advantage to one over the other?
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 11846908
while ($row=mysql_fetch_array($result, MYSQL_ASSOC)) {

is same as:

while ($row=mysql_fetch_assoc($result)) {

With mysql_fetch_array, you have additional options like MYSQL_ASSOC, MYSQL_NUM and MYSQL_BOTH. Thats it.

:-)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Importing and exporting data Magento 1.x ? 4 35
php mail headers 2 11
PHP and Soap 3 28
XML Data Missing in PHP SimpleXML 8 22
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now