Solved

foreach() loop giving double output?

Posted on 2004-08-19
4
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

752 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