4

I have 2 arrays:

$arr = [];

$tempArray = [
    'val1' => "xxx",
    'val2' => 0,
    'val3' => 0
];

Then in my mysql query i fill the temp array with values from the current row and finally push him into the $arr:

$stmt->bind_result($tempArray["val1"], $tempArray["val2"], $tempArray["val3"]);

while ( $stmt->fetch () ) {
    array_push($arr, $tempArray);
}

The Problem is, on every loop the "array_push" overrides the data in the $arr.

For example I loop 3 times in the $stmt->fetch().

1. Loop

$tempArray = [
    'val1' => "Hello",
    'val2' => 1,
    'val3' => 2
]

$arr = [
    0 = [
        'val1' => "Hello",
        'val2' => 1,
        'val3' => 2
    ];
]

2. Loop

$tempArray = [
    'val1' => "Stack",
    'val2' => 3,
    'val3' => 4
]

$arr = [
    0 = [
        'val1' => "Stack",
        'val2' => 3,
        'val3' => 4
    ],
    1 = [
        'val1' => "Stack",
        'val2' => 3,
        'val3' => 4
    ];
]

3. Loop

$tempArray = [
    'val1' => "Overflow",
    'val2' => 5,
    'val3' => 6
]

$arr = [
    0 = [
        'val1' => "Overflow",
        'val2' => 5,
        'val3' => 6
    ],
    1 = [
        'val1' => "Overflow",
        'val2' => 5,
        'val3' => 6
    ],
    2 = [
        'val1' => "Overflow",
        'val2' => 5,
        'val3' => 6
    ]
]

I never saw this behavior before and i don't know why it does this.

What i want at the end is this:

$arr = [
    0 = [
        'val1' => "Hello",
        'val2' => 1,
        'val3' => 2
    ],
    1 = [
        'val1' => "Stack",
        'val2' => 3,
        'val3' => 4
    ],
    2 = [
        'val1' => "Overflow",
        'val2' => 5,
        'val3' => 6
    ]
]

$stmt class (requested from @Stevish)

$query = "...";
if ( $stmt = $this->db->prepare($query)) {
        $stmt->bind_param('i',  $xxx);
        $stmt->execute();
        $stmt->store_result();
        $$stmt->bind_result($tempArray["val1"], $tempArray["val2"], $tempArray["val3"]);
        while ( $stmt->fetch () ) {
            $arr[] = $tempArr;
        }
    }
ajon
  • 7,868
  • 11
  • 48
  • 86
lhuber
  • 403
  • 1
  • 3
  • 13
  • Does this answer your question? [bind\_result into an array PHP mysqli prepared statement](https://stackoverflow.com/questions/4496994/bind-result-into-an-array-php-mysqli-prepared-statement) – Dharman May 05 '20 at 12:23

2 Answers2

0

Try:

while ( $stmt->fetch () ) {
    $arr[] = $tempArray;
}

The [] after the variable simply lets PHP know that you're adding a new entry to the array. It will be given a numerical value (0, 1, 2) as you require.

Stevish
  • 734
  • 5
  • 17
  • Same behavior as before, data gets overwritten. – lhuber Sep 19 '15 at 18:12
  • Ah, I see. I think it's in your $stmt->fetch() function that this is happening. Could you post the code for the $stmt class? – Stevish Sep 19 '15 at 18:13
  • Just looked at the man page and saw that your code and mine did the exact same thing, mine just had lower overhead – Stevish Sep 19 '15 at 18:14
  • i've added my class in the question above :) – lhuber Sep 19 '15 at 18:15
  • Sorry, mate. This is too far outside my area of knowledge. I'm not sure how to work with all that binding, executing and storing. I've not used those commands before. I use an entirely different approach – Stevish Sep 19 '15 at 18:29
  • This is never going to work. It doesn't solve the problem with references. – Dharman May 05 '20 at 12:18
0

The issue is that you are inserting a reference to $tempArray into $arr. Then you change the reference. By the third loop you have 3 references to the same array. That is why the values are showing that way... you can solve this in a rather non intuitive way.

try:

$stmt->bind_result($tempArray["val1"], $tempArray["val2"],$tempArray["val3"]);
while ( $stmt->fetch () ) {
    $x = $tempArray; //This copies the values of $tempArray to $x and each loop will create a new x.
    array_push($arr, $x);
}
ajon
  • 7,868
  • 11
  • 48
  • 86