What are some best practices for optimizing SQL queries in PHP to efficiently filter data based on specific options?

When filtering data based on specific options in SQL queries in PHP, it's important to use parameters in prepared statements to prevent SQL injection attacks and optimize query performance. Additionally, indexing the columns being filtered on can greatly improve query speed. It's also helpful to limit the number of columns being selected to only those needed for the query results.

// Example of optimizing SQL query in PHP to efficiently filter data based on specific options

// Define the specific options for filtering
$filterOption = 'some_value';

// Prepare the SQL query with a parameterized statement
$stmt = $pdo->prepare("SELECT * FROM table_name WHERE column_name = :filterOption");

// Bind the parameter value to the prepared statement
$stmt->bindParam(':filterOption', $filterOption);

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

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

// Loop through the results and do something with them
foreach ($results as $row) {
    // Process the data
}