What are some best practices for structuring SQL queries in PHP to retrieve data based on specific criteria?

When structuring SQL queries in PHP to retrieve data based on specific criteria, it is best practice to use prepared statements to prevent SQL injection attacks and ensure data integrity. Additionally, using placeholders for dynamic values in the query can make it easier to read and maintain the code.

// Example of structuring a SQL query in PHP to retrieve data based on specific criteria using prepared statements

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

// Define the criteria for retrieving data
$criteria = "example_criteria";

// Prepare the SQL query with a placeholder for the criteria
$stmt = $pdo->prepare("SELECT * FROM mytable WHERE column_name = :criteria");

// Bind the criteria value to the placeholder
$stmt->bindParam(':criteria', $criteria);

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

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

// Loop through the results and do something with the data
foreach ($results as $row) {
    echo $row['column_name'] . "<br>";
}