What are the recommended SQL query practices for selecting specific data in PHP scripts?

When selecting specific data in PHP scripts using SQL queries, it is recommended to use parameterized queries to prevent SQL injection attacks and to properly sanitize user input. Additionally, it is good practice to only select the necessary columns and to use proper error handling to deal with any potential issues that may arise during the query execution.

// Example of selecting specific data using parameterized queries in PHP
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Define the query with placeholders for parameters
$query = "SELECT column1, column2 FROM mytable WHERE id = :id";

// Prepare the query
$stmt = $pdo->prepare($query);

// Bind the parameter values
$id = 1;
$stmt->bindParam(':id', $id, PDO::PARAM_INT);

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

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

// Handle any errors
if (!$results) {
    echo "Error executing query";
} else {
    foreach ($results as $row) {
        // Process the data
        echo $row['column1'] . ' - ' . $row['column2'] . '<br>';
    }
}