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 8041863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:22:11+00:00 2026-06-05T04:22:11+00:00

This question is related to this previous question: PDO, MySQL – How do I

  • 0

This question is related to this previous question: PDO, MySQL – How do I return an array from a function?

But I felt it wasn’t specific enough.

I want to return each row of variables in its own key in an array so I can use them in the calling program.

This is the code I have so far but am not sure if its correct or even going in the right direction:

$total = $dbh->prepare("SELECT COUNT(post_id) FROM mjbox_posts");
        $stmt = $dbh->prepare("SELECT * FROM mjbox_images JOIN mjbox_posts USING (post_id) WHERE post_id = ?");
        $stmt->bindParam(1,$post_id);
        $stmt->execute();

        $resultarray = array();

            while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            for($i=0;$i<=$total;$i++){

                $resultarray[] = array("id"=>$row['img_id'],"filename"=>$row['img_file_name']);

            }

        }

        return $resultarray;

It appears the data does get inserted into the array and passed back to the calling program but it seems to duplicate each piece of data twice in the array:

Array ( [0] => Array ( [0] => Array ( [id] => 5 [filename] => fullsize/8520430289728860armor.jpg ) [1] => Array ( [id] => 5 [filename] => fullsize/8520430289728860armor.jpg ) [2] => Array ( [id] => 6 [filename] => fullsize/5535575154865203bg1.jpg ) [3] => Array ( [id] => 6 [filename] => fullsize/5535575154865203bg1.jpg ) [4] => Array ( [id] => 7 [filename] => fullsize/7598226850012898images.jpg ) [5] => Array ( [id] => 7 [filename] => fullsize/7598226850012898images.jpg ) ) )
  • 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-05T04:22:12+00:00Added an answer on June 5, 2026 at 4:22 am

    That code is broken. $total is going to be a mysql prepared statement handle, but you’re using it in an integer comparison in your inner for(...) loop.

    Your other query is wasting a lot of resources by fetching ALL the fields in your table, when you only need the two fields you’re saving into your array.

    Simplify that mess into:

    query:

    SELECT img_id AS id, img_file_name AS filename etc....
    

    php:

    $resultarray = array();
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
       $resultarray[] = $row;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is related to my previous question - MySQL query to show records
This is a CSS related question, I got one good answer from my previous
This question is related to a previous question I asked, but it's a different.
This question ties to my previous question but is more specific. Say I have
This is related to my previous question , but it seems I have another
This is related question to my previous question but different one. After searching a
This question is related to my previous question but you dont need to read
This question is related to a previous question of mine That's my current code
This question is related to my previous question How to generate Cartesian Coordinate (x,y)
This question is related to my previous question . The storyline: I have an

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.