How can PHP be utilized to directly select and display a single record based on an ID in a database?

To select and display a single record based on an ID in a database using PHP, you can use a SQL query with a WHERE clause that specifies the ID you want to retrieve. You can then fetch the result and display the record's information on the webpage.

<?php
// Connect to 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);
}

// Get record ID from URL parameter
$id = $_GET['id'];

// Select record from database
$sql = "SELECT * FROM table_name WHERE id = $id";
$result = $conn->query($sql);

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

$conn->close();
?>