这是我的 Php 代码。我是 php 新手,我想获得 JSON 的正确响应。
$db_handle = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $db_handle )
{
die('Could not connect: ' . mysql_error());
}
else
$response["message"] = "Connected Succsessfully";
//echo json_encode($response);
$db_found = mysql_select_db($db, $db_handle);
// check for required fields
if($db_found)
{
// mysql inserting a new row
mysql_query('SET CHARACTER SET utf8');
$query = "SELECT * FROM users";
$result = mysql_query($query);
//iterate over all the rows
$emparray[] = array();
while($row = mysql_fetch_assoc($result)){
//iterate over all the fields
$emparray[] = $row;
}
echo json_encode($emparray);
这是我的回应:
[[],{"id":"1","username":"Liyari","password":"Karachi"},{"id":"12","username":"ssssss","password":"111111"},{"id":"121","username":"135","password":"Qasim"},{"id":"17","username":"nafees","password":"99999"},{"id":"134","username":"","password":""},{"id":"133","username":"","password":""},{"id":"135","username":"mustafa","password":"45612"},{"id":"131","username":"mustafa","password":"45612"},{"id":"132","username":"","password":""}]
Q) 为什么我在 Response 开始时得到空数组。
最佳答案
移除
$emparray[] = array();
或者把它变成
$emparray = array();
然后它会正常工作。
关于PHP : json_encode is returning NULL array at the start of the string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32158891/