[英]encoding multidimensional array in PHP 7.0 doesn't work (json_encode)
I have a problem with getting JSON data from mysql phpmyadmin(Version 4.0) table, I have tried PDO and mysql_connect. 我从mysql phpmyadmin(Version 4.0)表获取JSON数据时遇到问题,我尝试了PDO和mysql_connect。
My idea: mysql--> PHP--> echo json 我的想法是:mysql-> PHP-> echo json
The connection to my server works and the SQL statement works perfectly. 与服务器的连接正常,SQL语句正常运行。 I have tested it. 我已经测试过了 But the "json_encode"(and some others) of the JSON itself is not possible. 但是JSON本身的“ json_encode”(以及其他一些符号)是不可能的。 The json array is not builted. json数组未构建。
Are there settings in PhpMyAdmin which I have to pay attention to? 我必须注意PhpMyAdmin中的设置吗?
PDO: PDO:
$query = $pdo->prepare('SELECT p.*, count(r.rate) AS rates, avg(r.rate) AS average from plugins p left join rate r on p.title = r.title group by p.title');
$query->execute();
$row = $query->fetchAll();
// send the data encoded as JSON
$json = json_encode($row, JSON_UNESCAPED_UNICODE);
echo $json;
print_r($row);
exit;
Result is: 结果是:
Array ( [0] => Array ( [id] => 153 [0] => 153 [title] =>
Where am I going wrong? 我要去哪里错了? I updated my server to PHP 7.0 and now the code doesn't work. 我将服务器更新为PHP 7.0 ,现在代码不起作用。 Before the update all is working perfectly and there was a long json array(how it should be) 在更新之前,一切工作正常,并且有一个很长的json数组(应该如何)
How it should be 应该如何
{{"id":"1","title":"ExmapleTitle"....},{"id":"2","title":"ExmapleTitle2"....}...}
You must call execute() 您必须调用execute()
<?php
$pdo = new PDO('mysql:host=xxxxx;dbname=xxxxx', 'xxxxxx', 'xxxxxx');
$statement=$pdo->prepare("SELECT p.*, count(r.rate) AS rates, avg(r.rate) AS average from plugins p left join rate r on p.title = r.title group by p.title");
$statement->execute(); // <<< --- You are missing this
$data = $statement->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($data); //Echo: data ... voila
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.