PHP的mysqli取"阵推"覆盖数据 [英] PHP MySQLi fetch "array push" overrides data
问题描述
我有两个数组:
$ ARR = [];$ tempArray = [
VAL1'=> XXX,
'val2的'=> 0,
VAL3'=> 0
];
然后在我的MySQL查询我充满价值的临时数组从当前行并最终推他到$ ARR:
$ stmt-> bind_result($ tempArray [VAL1],$ tempArray [val2的],$ tempArray [VAL3]);而($ stmt->取()){
array_push($改编,$ tempArray);
}
问题是,在每个循环的array_push中的 $改编
覆盖数据。
例如我循环3次在 $ stmt-方式>取()
1。循环
$ tempArray = [
VAL1'=> 你好,
'val2的'=> 1,
VAL3'=> 2
]$ ARR = [
0 = [
VAL1'=> 你好,
'val2的'=> 1,
VAL3'=> 2
];
]
2。循环
$ tempArray = [
VAL1'=> 堆栈,
'val2的'=> 3,
VAL3'=> 4
]$ ARR = [
0 = [
VAL1'=> 堆栈,
'val2的'=> 3,
VAL3'=> 4
]
1 = [
VAL1'=> 堆栈,
'val2的'=> 3,
VAL3'=> 4
];
]
3。循环
$ tempArray = [
VAL1'=> 溢出,
'val2的'=> 5,
VAL3'=> 6
]$ ARR = [
0 = [
VAL1'=> 溢出,
'val2的'=> 5,
VAL3'=> 6
]
1 = [
VAL1'=> 溢出,
'val2的'=> 5,
VAL3'=> 6
]
2 = [
VAL1'=> 溢出,
'val2的'=> 5,
VAL3'=> 6
]
]
我从来没有见过这之前的行为,我不知道为什么它这样做。
我想在最后是这样的:
$ ARR = [
0 = [
VAL1'=> 你好,
'val2的'=> 1,
VAL3'=> 2
]
1 = [
VAL1'=> 堆栈,
'val2的'=> 3,
VAL3'=> 4
]
2 = [
VAL1'=> 溢出,
'val2的'=> 5,
VAL3'=> 6
]
]
$语句级(从@Stevish要求)
$查询=...;
如果($语句= $这个 - > DB-GT&; prepare($查询)){
$ stmt-> bind_param('我',$ XXX);
$ stmt->执行();
$ stmt-> store_result();
$$ stmt-> bind_result($ tempArray [VAL1],$ tempArray [val2的],$ tempArray [VAL3]);
而($ stmt->取()){
$常用3 [] = $ tempArr;
}
}
的问题是,要插入参考$ tempArray到$编曲。然后,您更改参考。到了第三圈,你有3个引用同一个数组。这就是为什么值显示,这样...你可以在一个相当的非直观的方式解决这个问题。
尝试:
$ stmt-> bind_result($ tempArray [VAL1],$ tempArray [val2的],$ tempArray [VAL3]);
而($ stmt->取()){
$ X = $ tempArray; //这个副本$ tempArray的值$ x和每个回路将创建一个新的x。
array_push($改编,$ X);
}
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;
}
}
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);
}
这篇关于PHP的mysqli取"阵推"覆盖数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!