Here is my data.php
$global = $pdo->query("SELECT name, age");
$global = $global->fetchAll(PDO::FETCH_ASSOC);
$arr = array('data'=>$global);
echo json_encode($arr, JSON_PRETTY_PRINT);
Here is my index.php
<html>
<?php
$json_url = "data.php";
$json = utf8_encode(file_get_contents($json_url));
$data = json_decode($json, false);
echo "<pre>";
print_r($data);
echo "</pre>";
?>
</html>
I get blank data. What am I doing wrong?
First of all make sure file_get_contents is actually returning something. If not, check your logs to see what file its actually requesting. Use a full-path ex: http://location/path/to/data.php
I have had issues with reading JSON without setting the Content-Type. header('Content-Type: application/json')
Try this:
$query = "SELECT * FROM table";
$stmt = $pdo->query($query);
$global= $stmt->fetchAll(PDO::FETCH_ASSOC);
$arr = array('data'=>$global);
echo json_encode($arr);
//echo json_encode($arr, JSON_PRETTY_PRINT);
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.