我正在尝试从我的数据库中检索一个数组,但是一直没有返回NULL值。
我尝试使用UTF-8 unicode,众多不同方法和托管网站转换整个数据库。
我使用print_r和json_encode获得的响应如下:
Array
(
[success] =>
[0] => Array
(
[lat] =>
[lng] =>
[value] =>
)
[1] => Array
(
[lat] =>
[lng] =>
[value] =>
)
[2] => Array
(
[lat] =>
[lng] =>
[value] =>
)
[3] => Array
(
[lat] =>
[lng] =>
[value] =>
)
[4] => Array
(
[lat] =>
[lng] =>
[value] =>
)
)
0No error{"success":false,"0":{"lat":null,"lng":null,"value":null},"1"{"lat":null,"lng":null,"value":null},"2":{"lat":null,"lng":null,"value":null},"3":{"lat":null,"lng":null,"value":null},"4":{"lat":null,"lng":null,"value":null}}
我的PHP代码:
<?php
header('Content-Type: application/json');
$con = mysqli_connect("aaa", "bbb", "ccc", "ddd");
//Sets charset for JSON parsing
//no need anymore, database is utf8_unicode_ci already
//$charset = mysql_query("SET CHARACTER SET utf8");
//utf8_encode($arrItem);
$statement = mysqli_prepare($con, "SELECT * FROM markers ORDER BY marker_id");
mysqli_stmt_execute($statement) or die(mysqli_error($con));
$arrRows = array();
$arryItem = array();
$arrRows["success"] = false;
$arryItem["success"] = false;
mysqli_stmt_bind_result($marker_id,$lat,$lng,$value);
while(mysqli_stmt_fetch($statement)) {
$arrRows[] = compact('marker_id','lat','lng','value');
}
print_r($arrRows);
echo json_last_error();
echo json_last_error_msg();
echo json_encode($arrRows);
?>
应该返回的值是:
非常感谢所有帮助,真的需要解决这个问题!
如果有什么我没解释得不够或您需要其他信息,请告诉我。我可以发布我为while循环尝试的其他方法,但被告知使用compact()是最佳的方法。
答案 0 :(得分:1)
在以程序方式使用时,您需要将$statement
作为mysqli_stmt_bind_result()
来电的第一个参数传递。