foreach() loop giving double output?

Posted on 2004-08-19
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?
Question by:TunaMaxx
  • 3
LVL 32

Expert Comment

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

Change to :

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

Accepted Solution

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)) {

Author Comment

ID: 11846859

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?
LVL 32

Expert Comment

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.


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

920 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

17 Experts available now in Live!

Get 1:1 Help Now