我看到了 array_push php 函式的罕見行為。就像當我遍歷查詢的結果行并將完整的資料陣列推送到“父”陣列($rdo)中時,這些值正在修改為添加最后一行的值。
這是我的代碼:
$rdo = array();
if($sentencia = $general_con->prepare("SELECT monthly_price, name FROM subscription_plan WHERE deleted=0"))
{
$sentencia->execute();
$sentencia->store_result();
$num_of_rows = $sentencia->num_rows;
$sentencia->bind_result($row['monthly_price'], $row['name']);
while($sentencia->fetch())
{
array_push($rdo, $row);
echo print_r($rdo,1).'<br/><br/>';
}
$sentencia->close();
die();
}
這是結果:

uj5u.com熱心網友回復:
看起來這是bind_result()使用對陣列元素的參考的方式的產物。當您按下 時$row,$rdo這將更新所有這些陣列元素。
我建議你遠離 usingbind_result()和 use fetch_assoc()。
if($sentencia = $general_con->prepare("SELECT monthly_price, name FROM subscription_plan WHERE deleted=0"))
{
$sentencia->execute();
$result = $sentencia->get_result();
while($row = $result->fetch_assoc())
{
array_push($rdo, $row);
echo print_r($rdo,1).'<br/><br/>';
}
$sentencia->close();
die();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/421715.html
標籤:
