Create Dynamic JSON File in PHP

 <?php


$servername = "localhost";
$username = "root";
$password = "";
$database = "test";

//Create Connection
$conn = new mysqli($servername, $username, $password, $database);

//Check Connection
if ($conn->connect_error) {
    die("Connection Failed: " . $conn->connect_error);
}
Above file is mysqli_conn.php File which is included in below files.






Below is daily-json-file.php File
<?php

include("mysqli_conn.php");

$sql = "SELECT * FROM students";
$result = $conn->query($sql);

$output = $result->fetch_all(MYSQLI_ASSOC);

$json_data = json_encode($output, JSON_PRETTY_PRINT);

$file_name = "my-" . date("d-m-Y") . ".json";

if (file_put_contents("json/{$file_name}", $json_data)) {
    echo $file_name . "File created.";
} else {
    echo "Can't Insert data in JSON File.";
}


Comments

Popular posts from this blog

Ajax Insert Records in Database Tutorial in PHP

SubQuery with EXISTS and NOT EXISTS in PHP

Submitting HTML Form data in JSON File (without MySql Database) in PHP