How can PHP be used to retrieve specific data from a database based on a given ID parameter?

To retrieve specific data from a database based on a given ID parameter, you can use SQL queries in PHP to fetch the desired data. You can use a prepared statement to prevent SQL injection attacks and bind the ID parameter to the query. Once the query is executed, you can fetch the result and use it as needed in your PHP code.

<?php
// Database connection
$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);
}

// Retrieve data based on ID parameter
$id = $_GET['id']; // Assuming the ID parameter is passed through the URL

$stmt = $conn->prepare("SELECT * FROM your_table WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();

// Fetch and display the data
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "ID: " . $row["id"] . " - Name: " . $row["name"];
    }
} else {
    echo "No results found";
}

$stmt->close();
$conn->close();
?>