Output json with php

How to generate JSON data with PHP?

To generate JSON in PHP, you need only one function, json_encode().

When working with database, you need to get all the rows into array first. Here is a sample code for mysqli

$sql="select * from Posts limit 20"; $result = $db->query($sql); $posts = $result->fetch_all(MYSQLI_ASSOC); 

then you can either use this array directly or make it part of another array:

echo json_encode($posts); // or $response = json_encode([ 'posts' => $posts, ]); 

if you need to save it in a file then just use file_put_contents()

file_put_contents('myfile.json', json_encode($posts)); 

I used this code on my project. results.json file works well on local host and fails on remote server. Can you explain why so..

allthough this is great maybe because I’m using PHP 5 now and it wasn’t available at the time this answer was posted but you can get rid of the $result= line and inside your while loop just make the mysql_feth_array($sql)

fwrite($fp, json_encode($response,JSON_PRETTY_PRINT)); — uses whitespace to format the JSON. Other constants here

$json_data = json_encode($posts); file_put_contents('myfile.json', $json_data); 

You can create the myfile.json before you run the script.But its not compulsory if you have full sudo privileges(read/write permissions(For of you on Mac).

Here is a working Example:

 Array ( "id" => "01", "title" => "Hello", ), "1" => Array ( "id" => "02", "title" => "Yoyo", ), "2" => Array ( "id" => "03", "title" => "I like Apples", ) ); // encode array to json $json = json_encode($posts); $bytes = file_put_contents("myfile.json", $json); //generate json file echo "Here is the myfile data $bytes."; ?> 

Example2: Another example with a json data and error handling.

 "01", "title" => "Hello", ], [ "id" => "02", "title" => "Yoyo", ], [ "id" => "03", "title" => "I like Apples", ] ]; // Encode array to JSON with formatting $json = json_encode($posts, JSON_PRETTY_PRINT); // Write JSON to file and handle errors $file = "myfile.json"; if (file_put_contents($file, $json) !== false) < echo "Data has been written to $file."; >else < echo "Error occurred while writing to $file."; >?> 

When JSON_PRETTY_PRINT is passed as the second argument to json_encode(), it tells the function to add whitespace and formatting to the generated JSON string. This makes the resulting JSON structure easier to read and understand when viewing it directly.

Читайте также:  Тег SCRIPT

Example3: With SQL data from a server:

connect_error) < die("Connection failed: " . $conn->connect_error); > // Fetch data from the database $sql = "SELECT id, name, email FROM users"; $result = $conn->query($sql); if ($result->num_rows > 0) < $users = array(); while ($row = $result->fetch_assoc()) < $users[] = $row; >// Encode SQL data to JSON $json = json_encode($users, JSON_PRETTY_PRINT); // Write JSON to file $file = "users.json"; if (file_put_contents($file, $json) !== false) < echo "Data has been written to $file."; >else < echo "Error occurred while writing to $file."; >> else < echo "No data found."; >// Close the database connection $conn->close(); ?> 

Источник

Оцените статью