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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:58:40+00:00 2026-05-23T17:58:40+00:00

I’ve been reaching the end of my ideas with this crazy problem. Using mysqli

  • 0

I’ve been reaching the end of my ideas with this crazy problem.

Using mysqli I run the following query

SELECT * FROM `shop_cart` WHERE `tmpID`=?

It should return each row in an array like this:

Array
(
    [0] => Array
        (
            [id] => 1
            [tmpID] => af83abab7fdee8eb0cf8919f171cdeec
            [pID] => 800
            [qty] => 2
            [time] => 1310076898
        )

    [1] => Array
        (
            [id] => 2
            [tmpID] => af83abab7fdee8eb0cf8919f171cdeec
            [pID] => 797
            [qty] => 1
            [time] => 1310076903
        )

    [2] => Array
        (
            [id] => 3
            [tmpID] => af83abab7fdee8eb0cf8919f171cdeec
            [pID] => 883
            [qty] => 1
            [time] => 1310076907
        )

    [3] => Array
        (
            [id] => 4
            [tmpID] => af83abab7fdee8eb0cf8919f171cdeec
            [pID] => 795
            [qty] => 1
            [time] => 1310076909
        )

)

Instead I get four copies of the same row like this:

Array
(
    [0] => Array
        (
            [id] => 4
            [tmpID] => af83abab7fdee8eb0cf8919f171cdeec
            [pID] => 795
            [qty] => 1
            [time] => 1310076909
        )

    [1] => Array
        (
            [id] => 4
            [tmpID] => af83abab7fdee8eb0cf8919f171cdeec
            [pID] => 795
            [qty] => 1
            [time] => 1310076909
        )

    [2] => Array
        (
            [id] => 4
            [tmpID] => af83abab7fdee8eb0cf8919f171cdeec
            [pID] => 795
            [qty] => 1
            [time] => 1310076909
        )

    [3] => Array
        (
            [id] => 4
            [tmpID] => af83abab7fdee8eb0cf8919f171cdeec
            [pID] => 795
            [qty] => 1
            [time] => 1310076909
        )

)

The problem lies somewhere in this bit of code I think:

while ($query->fetch()){ 
    $results[] = $fields;
}

If I put print_r($fields) in like this:

while ($query->fetch()){ 
    print_r($fields);
    $results[] = $fields;
}

It prints each row of the results correctly. But if I put print_r($results) in here like this:

while ($query->fetch()){ 
    $results[] = $fields;
}
print_r($results);

…then I get one large array containing duplicate copes of only one row. It seems to me that the $results array isn’t getting populated correctly. It seems that the data is coming out of the database okay.

Any help would be great, I’m really at the end of my rope at figuring this out!

Edit

Here’s some more code preceding (and including) the fetch() loop as posted above.

// Generate Types
$types = '';                        //initial sting with types
foreach($params as $param) {        //for each element, determine type and add
    if(is_int($param)) {
        $types .= 'i';              //integer
    } elseif (is_float($param)) {
        $types .= 'd';              //double
    } elseif (is_string($param)) {
        $types .= 's';              //string
    } else {
        $types .= 'b';              //blob and unknown
    }
}
array_unshift($params, $types);

$query = $this->connection->stmt_init();
if($query->prepare($sql)) {
    call_user_func_array(array($query,'bind_param'),$this->refValues($params));
    $query->execute(); 

    if($fetch){ // Only if we want to return an array of results
        // Get Metadata
        $meta = $query->result_metadata();

        $fields = $results = array();
        while ($field = $meta->fetch_field()) { 
            $var = $field->name; 
            $$var = null; 
            $fields[$var] = &$$var; 
        }

        call_user_func_array(array($query,'bind_result'),$fields);

        while ($query->fetch()){ 
            pr($fields);
            $results[] = $fields;
        }
    }
} else die(printf("Error: %s\n", $this->connection->error.' : '.$this->lastQ));
  • 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-05-23T17:58:41+00:00Added an answer on May 23, 2026 at 5:58 pm

    Okay, I’ve worked out a solution. Albeit not a very elegant one, but it works :). I’m just making an array called $fieldNames and storing the names of the fields being returned via mysql. Then in the fetch() while loop I’m just looping through the $fieldNames and setting $results from $fields[$fieldNames]. The code below might make more sense than I can make by explaining it!

    $fields = $results = array();
    while ($field = $meta->fetch_field()) { 
        $var = $field->name; 
        $fields[$var] = &$$var; 
        $fieldNames[] = $var;
    }
    
    $fieldCount = count($fieldNames);
    
    call_user_func_array(array($query,'bind_result'),$fields);
    
    $i=0;
    while ($query->fetch()){
        $key = $arKey ? $fields[$arKey] : $i;
        for($l=0;$l<$fieldCount;$l++) $results[$key][$fieldNames[$l]] = $fields[$fieldNames[$l]];
        $i++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.