【问题标题】:Pass sql data into associative array in PHP将sql数据传递到PHP中的关联数组中
【发布时间】:2016-06-01 08:14:26
【问题描述】:

我正在尝试将一些数据库条目打印到关联数组中,但它只打印第一个条目。

这是我的代码

if (mysqli_num_rows($result) > 0) {
    $data_array = array();
    while($row = mysqli_fetch_assoc($result)) { 
        $data_array['name']=$row["name"];
        $data_array['title']=$row["title"];
        $date=date_create($row['published']);
        $edited_date=date_format($date,"l, F d, Y");
        $data_array['date']=$edited_date; 
    }
} else {
    echo "0 results";
} 
echo json_encode($data_array);

【问题讨论】:

    标签: php sql arrays associative-array


    【解决方案1】:

    您每次都在替换数据。您需要为数组添加更多深度:

    while($row = mysqli_fetch_assoc($result)) { 
        $data_array[]['name']=$row["name"];
        $data_array[]['title']=$row["title"];
        $date=date_create($row['published']);
        $edited_date=date_format($date,"l, F d, Y");
        $data_array[]['date']=$edited_date; 
    }
    

    【讨论】:

    • 感谢您拯救我的一天,您是我的英雄 :)
    • 有一天我也会成为某人的英雄。
    猜你喜欢
    • 2013-03-21
    • 1970-01-01
    • 2018-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多