I have a problem with getting JSON data from mysql phpmyadmin(Version 4.0) table, I have tried PDO and mysql_connect.
My idea: mysql--> PHP--> echo json
The connection to my server works and the SQL statement works perfectly. I have tested it. But the "json_encode"(and some others) of the JSON itself is not possible. The json array is not builted.
Are there settings in PhpMyAdmin which I have to pay attention to?
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. Before the update all is working perfectly and there was a long json array(how it should be)
How it should be
{{"id":"1","title":"ExmapleTitle"....},{"id":"2","title":"ExmapleTitle2"....}...}
You must call 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.