How can PHP be used to retrieve specific data from a database based on user input?

To retrieve specific data from a database based on user input in PHP, you can use SQL queries with placeholders for user input values. This helps prevent SQL injection attacks and allows for dynamic querying based on user input. You can then bind the user input values to the placeholders before executing the query to retrieve the desired data from the database.

<?php
// Assuming $userInput contains the user input value
$userInput = $_POST['user_input'];

// Establish a database connection
$pdo = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');

// Prepare a SQL query with a placeholder for user input
$stmt = $pdo->prepare("SELECT * FROM your_table WHERE column_name = :user_input");

// Bind the user input value to the placeholder
$stmt->bindParam(':user_input', $userInput);

// Execute the query
$stmt->execute();

// Fetch the results
$results = $stmt->fetchAll();

// Process the retrieved data as needed
foreach ($results as $row) {
    // Do something with the data
}
?>