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

  • Home
  • SEARCH
  • 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 7953295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:07:12+00:00 2026-06-04T03:07:12+00:00

Trying to return a DB resultset as an array in my DAO: I want

  • 0

Trying to return a DB resultset as an array in my DAO:

I want to append string key-values to the array $retval in the below code. However, the array keeps getting overwritten each iteration instead of being appended to.

So at the end of the loop, I end up with 1 key-value instead of n pairs (n rows retrieved from the database). What am I doing wrong?

$retval = array();
while ($row = mysql_fetch_assoc($result)) {

    foreach($columns as $var) {
        $retval[$var]=$row[$var];
    }

}
var_dump($retval);

$retval ends up as ["name"=>"Japan","capital"=>"Tokyo"] instead of the expected ["name"=>"Korea","capital"=>"Seoul"...."Japan"=>"Tokyo"] where columns are name and capital.

  • 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-04T03:07:13+00:00Added an answer on June 4, 2026 at 3:07 am

    It is because you are overwriting the same value (column name here.) Something like:

    $retval['a'] = 1;
    $retval['a'] = 2;
    // ...
    

    Instead use:

    $retval[] = $row[$var];
    

    HTH.

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

Sidebar

Related Questions

I'm trying to do something like this: public void <String,int> getItem { return <Jen,23>;
I am trying to return my SQL query array into a javascript array and
I am trying to return a list of ungrouped values having a max defined
I need to return multiple STRING values from my backgroundworker in each loop, so
I am trying to return a custom query, which renders each row once, however
I'm trying to have this function return multiple results in array(?). So basically instead
So I'm trying to find a way to return a very large result set
I am trying to return results from my database so that I can create
I'm trying to make a simple search and return results in a paginated form.
I'm trying to post some form data and return results but I am having

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.