PHP的mysqli取"阵推"覆盖数据 [英] PHP MySQLi fetch "array push" overrides data

查看:100
本文介绍了PHP的mysqli取"阵推"覆盖数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组:

  $ 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆