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;
}
}