Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP/MySQL:SyntaxError:JSON.parse:JSON数据第1行第1列的意外字符_Php_Mysql_Json_Pdo - Fatal编程技术网

PHP/MySQL:SyntaxError:JSON.parse:JSON数据第1行第1列的意外字符

PHP/MySQL:SyntaxError:JSON.parse:JSON数据第1行第1列的意外字符,php,mysql,json,pdo,Php,Mysql,Json,Pdo,我有一张MySQL表: mysql> select * from members; +-------+-----------+-----------+ | memid | firstname | lastname | +-------+-----------+-----------+ | 1 | billal | begueradj | | 2 | bill | gates | | 3 | steve | jobs |

我有一张MySQL表:

mysql> select * from  members;
+-------+-----------+-----------+
| memid | firstname | lastname  |
+-------+-----------+-----------+
|     1 | billal    | begueradj |
|     2 | bill      | gates     |
|     3 | steve     | jobs      |
+-------+-----------+-----------+
3 rows in set (0.00 sec)
我有以下代码:

<?php
$output = array('error' => false);
$members = array();

try {
    $db = new PDO('mysql:host=localhost;dbname=bill;charset=utf8',
                   'root',
                   ''
    );
} catch(Exception $e) {
    die('Error in connecting to DB: <br/>'.$e->getMessage());
}

$response = $db->query('SELECT * FROM members');
while($row = $response->fetch()){
    echo $row['firstname'].' ';
    echo $row['lastname'].'<br/>';
    array_push($members, $row);
}

$output['members'] = $members;
$response->closeCursor();
$json = json_encode($out);
echo $json; // outputs correctly
header("Content-type: application/json");    // error here
die();
?>

仍然会收到相同的错误消息

请尝试此操作,它可能会帮助您解决此问题

if ($response->num_rows > 0) {
        while($row[] = $response -> fetch_assoc()) {
            $item = $row;
            $json = json_encode($item);
        }
    }
header()
应该放在所有输出之上,所以在回显任何内容之前

另外,由于您没有将整个内容编码为JSON,而只是部分内容,我猜您缺少JSON的开头和结尾

正确的JSON输出:

例1:

例2:

PHP:


你真的回显了json吗?所以在
header()
行之后,你尝试了
回显$json出现错误?您尝试了
ehco
而没有
echo
^噢。。。是的,对不起。。。现在它输出正确,但错误仍然存在,将
echo
放在
header()
行之后。这与我在问题中发布的错误消息相同
if ($response->num_rows > 0) {
        while($row[] = $response -> fetch_assoc()) {
            $item = $row;
            $json = json_encode($item);
        }
    }
{
  "key1": "value1",
  "key2": "value2",
  "key3": "value3"
}
[
  {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3"
  },
  {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3"
  },
  {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3"
  }
]
<?php

$output = ["error" => false, "members" => []];

try {
  $db = new PDO("mysql:host=localhost;dbname=bill;charset=utf8", "root", "");
} catch(Exception $e) {
  die("Error in connecting to DB: <br/>{$e->getMessage()}");
}

$response = $db->query("SELECT * FROM members");

while($row = $response->fetch(PDO::FETCH_ASSOC)) {
  array_push($output["members"], $row);
}

$response->closeCursor();

$json = json_encode($output);

header("Content-type: application/json");

echo $json;