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

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