How can the LOCATE function be utilized in PHP to improve data retrieval from a MySQL database?

When retrieving data from a MySQL database in PHP, the LOCATE function can be used to search for a substring within a larger string. This can be helpful when filtering or searching for specific data based on a partial match. By using LOCATE in your SQL query, you can improve the accuracy and efficiency of data retrieval.

// Example of using LOCATE function in a SQL query to retrieve data from a MySQL database
$search_term = 'example';
$sql = "SELECT * FROM table_name WHERE LOCATE('$search_term', column_name) > 0";
$result = mysqli_query($connection, $sql);

// Loop through the results
while ($row = mysqli_fetch_assoc($result)) {
    // Process each row as needed
}