I created a mySQL database with phpMyAdmin in my local server. In this database I stored the names and the location of my friends (along with an id as a primary key for the database). I wrote and run the following php script to retrieve these data from the database and project them on my local web server (XAMPP):
<?php
$dbServername = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbName = 'Friends';
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
header('Content-Type: application/json');
$sql = 'SELECT * FROM friends;';
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo json_encode($row, JSON_PRETTY_PRINT);
}
}
However, in this way I take this output:
{
"id": "1",
"name": "David Belton",
"location": "New Haven"
}{
"id": "2",
"name": "Alex Danson",
"location": "New York"
}
which is not a valid json output overall. I would like to have the following output:
[{
"id": "1",
"name": "David Belton",
"location": "New Haven"
}, {
"id": "2",
"name": "Alex Danson",
"location": "New York"
}]
(which is also a valid json output)
How can I do this?