API and Databases in PHP
Often, an API is used to retrieve data from a database. Let's look at an example.
Suppose we have a table with users. Let's create an API that will take a user's id as a parameter and return the data from the database for that user:
<?php
$id = $_GET['id'];
$query = "SELECT * FROM users WHERE id=$id";
$result = mysqli_query($link, $query);
$user = mysqli_fetch_assoc($result);
header('Content-Type: application/json');
echo json_encode($user, true);
?>
Suppose the database stores countries and their cities. Create an API that will take a country as a parameter and return an array of its cities.