How can PHP be used to make database content available for other websites?

To make database content available for other websites using PHP, you can create an API that fetches the data from the database and returns it in a format that can be easily consumed by other websites. This API can be accessed by making HTTP requests to a specific endpoint.

<?php

// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Fetch data from the database
$sql = "SELECT * FROM table_name";
$result = $conn->query($sql);

// Return data in JSON format
$data = array();
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $data[] = $row;
    }
}

header('Content-Type: application/json');
echo json_encode($data);

$conn->close();

?>