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
这是因为:
$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]
不存在。其余的是因为您试图对一个没有意义的数组进行字符串附加。
Notice: Array to string conversion in /index.php on line 141
这是因为
echo $locations;
. 不能将数组转换为字符串。你需要使用
var_dump
或
print_r
.
关于如何做到这一点。第一,
turn on mysqli exceptions so you don't have to constantly check for error
.
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
然后遍历行
pushing
上的新结果
locations
使用
locations[] = array(...)
.
$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
,您现在可以通过
fetch_all
而且速度有点快。
$locations = $result->fetch_all;