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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Log in through ID 5 36
Codeigniter Query Help Returning JSON 4 47
Generate PDF from MySQL using PHP 3 53
blocking the posted data from outside of my domains 8 21
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

756 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