What is the recommended method for querying and displaying data from a server on a PHP website?

To query and display data from a server on a PHP website, the recommended method is to use PHP's built-in MySQLi extension or PDO (PHP Data Objects) to connect to the database, execute SQL queries, fetch the results, and then display the data on the website using HTML or other suitable methods.

<?php
// Establish a connection to the database using MySQLi
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

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

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

// Query the database and fetch results
$sql = "SELECT * FROM table_name";
$result = $conn->query($sql);

// Display the data on the website
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "ID: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
    }
} else {
    echo "0 results";
}

// Close the connection
$conn->close();
?>