I am trying to generate a json file with the data from a mysql table via a query.
This is what have:
<?php
$servername = "localhost";
$username = "myuser";
$password = "mypass";
$dbname = "mydatabase";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM mytable";
$result = mysqli_query($conn, $sql);
$json_data = json_encode($result);
file_put_contents('myfile.json', $json_data);
?>
But it's generating this, instead of the actual data:
{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}
Table fields are: id, title and name
How can I fix this?
$json_data = json_encode(mysqli_fetch_array($result));