Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8272859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:16:21+00:00 2026-06-08T07:16:21+00:00

I have some PHP code which: connects to mysql table retrieves results formats results

  • 0

I have some PHP code which:

  1. connects to mysql table
  2. retrieves results
  3. formats results for display as HTML table (add tags, etc)
  4. stores the properly formatted results into a var named $echoResultsFinal

Then, inside of the HTML, I have some inline PHP. it:

  1. checks to see if $echoResultsFinal isset
  2. if it is, echo’s out its contents
  3. if not, echos out some stuff unrelated to the question

The problem: When the PHP generated table is echoed out, the table contents are not in the proper order.

The code which generates the table:

$echoResultArray = array();
$i = 0;
while ($row = mysql_fetch_array($result))
{
    $td = '';
    foreach(array('FIRST_NAME', 'LAST_NAME', 'RIT_ACC', 'LINK_1', 'LINK_2', 'LINK_3', 'LINK_4', 'LINK_5', 'LINK_6' ) as $key => $value)
    {
        $td .= "<td>" . $row[$key] . "</td>";
    }
    $echoResultArray[$i] = "<tr>" . $td . "</tr>";
    $i++;
}
//Table closing tag
$echoResultsClosing = "</tbody></table>";
mysql_close();

$echoResultData = '';
foreach($echoResultArray as $var)
{
    $echoResultData .= $var;
    $echoResultData .= PHP_EOL;
}

$echoResultFinal = $echoResult . $echoResultData . $echoResultsClosing;

Any idea what’s up? 🙂

Thanks for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:16:23+00:00Added an answer on June 8, 2026 at 7:16 am

    There is a problem in your first foreach loop. The code you have is this:

    foreach(array('FIRST_NAME', 'LAST_NAME', 'RIT_ACC', 'LINK_1', 'LINK_2', 'LINK_3', 'LINK_4', 'LINK_5', 'LINK_6' ) as $key => $value)
    {
        $td .= "<td>" . $row[$key] . "</td>";
    }
    

    The important thing to note here is that your foreach is assigning all the keys to $key and all of the values to $value as it iterates through the array you passed it ($key => $value). The problem here is you are using the $key to retrieve values from the $row array. However, with the array passed in, the keys are the numbers 0-8 (the indices of the array). You can see this by doing a var_dump of the array you pass to foreach:

    array(9) {
      [0] => string(10) "FIRST_NAME"
      [1] => string(9) "LAST_NAME"
      [2] => string(7) "RIT_ACC"
      [3] => string(6) "LINK_1"
      [4] => string(6) "LINK_2"
      [5] => string(6) "LINK_3"
      [6] => string(6) "LINK_4"
      [7] => string(6) "LINK_5"
      [8] => string(6) "LINK_6"
    }
    

    The $keys that you are using are in brackets. So you are looking for numeric indexes in the $row array. This still gives you output because of the default behavior of mysql_fetch_array():

    The type of returned array depends on how result_type is defined. By using MYSQL_BOTH (default), you’ll get an array with both associative and number indices. Using MYSQL_ASSOC, you only get associative indices (as mysql_fetch_assoc() works), using MYSQL_NUM, you only get number indices (as mysql_fetch_row() works).

    I’ve added the emphasis here to the quote from the docs.

    So your call to mysql_fetch_array() returns an array with column names and values as well as a numeric index of the columns and their values. Your results were out of order because your query’s SELECT ordered the fields in a different order than in the array you passed to foreach. Since the numeric index would correspond to the order of the fields in the SELECT, you were outputting them in the order the database gave them to you rather than the order you wanted.

    To avoid this bug in the future, explicitly use mysql_fetch_assoc() or mysql_fetch_row() or pass the MYSQL_ASSOC or MYSQL_NUM constants to mysql_fetch_array(). However, the mysql_* functions have been deprecated and will be removed in PHP 5.4. I highly suggest you look into PDO. (PDO has a similar return strategy which returns the column names and indices like MYSQL_BOTH so be careful!)

    To fix this, you should be using the $value to get the column from the query because this variable will contain the string name of the columns.

    foreach(array('FIRST_NAME', 'LAST_NAME', 'RIT_ACC', 'LINK_1', 'LINK_2', 'LINK_3', 'LINK_4', 'LINK_5', 'LINK_6' ) as $value)
    {
        $td .= "<td>" . $row[$value] . "</td>";
    }
    

    Also note that I changed your as $key => $value to just as $value because if you don’t use the key, you don’t need to specify a variable to receive it. Those are the two forms of foreach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some PHP code which allows me to sort a column into ascending
I have some PHP code which stores whatever is typed in a textbox in
I have some php files in a server which generate xml code in them.
I have some PHP code that generates a calendar and then outputs html to
We have a loop in our PHP code which inserts rows into a table.
I have some PHP code that generates dynamic tables of data on the fly.
I have some PHP code that is designed to make a spreadsheet with formulas
So I have some PHP code that looks like: $message = 'Here is the
I have some basic PHP code: $raceramps56[short] = My Test Product; $leftMenu = '<div
In a recent project, I have to maintain some PHP code. I set up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.