Php 将结果查询回送到数组

Php 将结果查询回送到数组,php,sql,Php,Sql,我想将查询结果回显到数组中 以下是原始行: { $locations = array(); $locations[0] = array("image"=>"https://www.XX.nl/assets/img/prinsenhof.png","lat"=>"52.012136","lng"=>"4.354596","emp_name"=>"Museum Prinsenhof Delft","emp_id"=>"Museum Prinsenhof Delft");

我想将查询结果回显到数组中

以下是原始行:

{
$locations = array();
$locations[0] = array("image"=>"https://www.XX.nl/assets/img/prinsenhof.png","lat"=>"52.012136","lng"=>"4.354596","emp_name"=>"Museum Prinsenhof Delft","emp_id"=>"Museum Prinsenhof Delft");
$locations[1] = array("image"=>"https://www.XX.nl/assets/img/ampelman.png","lat"=>"51.8988595","lng"=>"4.4186571","emp_name"=>"Ampelmann Operations","emp_id"=>"Ampelmann Operations");
$locations[2] = array("image"=>"https://www.XX.nl/assets/img/koekamp.png","lat"=>"52.0821565","lng"=>"4.3202341","emp_name"=>"Koekamp The Hague","emp_id"=>"Koekamp The Hague");
$locations[3] = array("image"=>"","lat"=>"51.9179543","lng"=>"4.3986012","emp_name"=>"Grote of Sint Janskerk Schiedam","emp_id"=>"Grote of Sint Janskerk Schiedam");
$locations[4] = array("image"=>"","lat"=>"52.0596095","lng"=>"4.2219163","emp_name"=>"The International School of The Hague","emp_id"=>"The International School of The Hague");
$locations[5] = array("image"=>"","lat"=>"52.012663","lng"=>"4.3558941","emp_name"=>"Oude Kerk","emp_id"=>"Oude Kerk");
$locations[6] = array("image"=>"","lat"=>"52.0769897","lng"=>"4.3170919","emp_name"=>"Spuiplein","emp_id"=>"Spuiplein");
$locations = json_encode($locations);
}
所以我把这个信息放在数据库里,我想显示它 这就是我对它的理解:

    $qry = "SELECT image, lat, lng, name, emp_id FROM googlemaps";
        if(!$result = $connection->query($qry)) {
            echo 'Fout in query: '.$mysqli->error;
        } else {
        $a = 0; 
        while ($location = $result->fetch_assoc()){

    $locations[$a] .= array("image"=>"https://www.XXXX.nl/assets/img/".$location['image'],"lat"=>$location['lat'],"lng"=>$location['lng'],"emp_name"=>$location['name'],"emp_id"=>$location['emp_id']);
    $a++;
            }
        }
        echo $locations;
echo $locations = json_encode($locations);
有没有更好的办法

错误:

Notice: Undefined offset: 0 in /index.php on line 137
Notice: Array to string conversion in /index.php on line 137
Notice: Array to string conversion in /index.php on line 137
Notice: Array to string conversion in /index.php on line 141
这是因为:

$locations[$a] .= array("image"=>"https://www.XXXX.nl/assets/img/".$location['image'],"lat"=>$location['image'],"lng"=>$location['lng'],"emp_name"=>$location['name'],"emp_id"=>$location['emp_id']);
第一个是告诉您,
$locations[0]
不存在。其余的是因为您试图将字符串附加到数组中,这是没有意义的

这是因为
echo$位置。不能只是将数组转换为字符串。你需要使用或


关于如何做到这一点。首先

然后使用
locations[]=array(…)
位置上迭代新结果行

通过在查询中执行所有操作,可以提高效率

$result = $connection->query(<<<QUERY
    SELECT
        CONCAT("https://www.XXXX.nl/assets/img/", image),
        lat,
        lng,
        name as emp_name,
        emp_id
    FROM googlemaps
QUERY
);
while ($location = $result->fetch_assoc()){
    # now use $location directly
}

我不清楚你想在这里完成什么。我想您是在尝试将静态数组更改为从数据库查询的数组?你能展示一下你期望的结果吗?我希望我现在把帖子说得更清楚了。
Notice: Array to string conversion in /index.php on line 141
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$result = $connection->query("SELECT image, lat, lng, name, emp_id FROM googlemaps");
while ($location = $result->fetch_assoc()){
    $locations[] = array(
        "image" =>  "https://www.XXXX.nl/assets/img/".$location['image'],
        "lat"   =>  $location['lat'],
        "lng"   =>  $location['lng'],
        "emp_name"  =>  $location['name'],
        "emp_id"    =>  $location['emp_id']
    );
}
$result = $connection->query(<<<QUERY
    SELECT
        CONCAT("https://www.XXXX.nl/assets/img/", image),
        lat,
        lng,
        name as emp_name,
        emp_id
    FROM googlemaps
QUERY
);
while ($location = $result->fetch_assoc()){
    # now use $location directly
}
$locations = $result->fetch_all;