What are the best practices for optimizing search queries in PHP when using PostgreSQL as the database?

When optimizing search queries in PHP with PostgreSQL, it is important to use parameterized queries to prevent SQL injection attacks and improve performance. Additionally, utilizing indexes on columns involved in search conditions can significantly speed up query execution. Finally, consider using full-text search capabilities provided by PostgreSQL for more advanced search functionality.

<?php
// Establish a connection to the PostgreSQL database
$pdo = new PDO('pgsql:host=localhost;dbname=mydatabase', 'username', 'password');

// Prepare a parameterized query with placeholders for search criteria
$stmt = $pdo->prepare('SELECT * FROM mytable WHERE column_name = :search_term');

// Bind the search term to the placeholder and execute the query
$search_term = 'example';
$stmt->bindParam(':search_term', $search_term);
$stmt->execute();

// Fetch and process the results
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($results as $row) {
    // Process each row as needed
}
?>