How can I optimize my PHP code to improve the search functionality for specific strings in a database?

To optimize the search functionality for specific strings in a database, you can use SQL queries with appropriate indexes and conditions to narrow down the search results. Additionally, you can use PHP functions like mysqli_real_escape_string() to prevent SQL injection attacks and improve the security of your code.

// Assuming $searchTerm is the specific string you want to search for in the database

// Connect to the database
$connection = mysqli_connect("localhost", "username", "password", "database");

// Escape the search term to prevent SQL injection
$searchTerm = mysqli_real_escape_string($connection, $searchTerm);

// Perform a SQL query to search for the specific string in the database
$query = "SELECT * FROM table_name WHERE column_name LIKE '%$searchTerm%'";
$result = mysqli_query($connection, $query);

// Fetch and display the search results
while ($row = mysqli_fetch_assoc($result)) {
    echo $row['column_name'] . "<br>";
}

// Close the database connection
mysqli_close($connection);