How can the FIELD function be used in PHP queries to specify a custom order for sorting results?

When sorting query results in PHP, the FIELD function can be used to specify a custom order for sorting based on a specific field's values. This is useful when you want to sort results in a non-traditional way, such as sorting by a specific list of values rather than ascending or descending order. By using the FIELD function in the ORDER BY clause of your SQL query, you can achieve this custom sorting behavior.

// Example query using FIELD function to specify custom sorting order
$query = "SELECT * FROM table_name ORDER BY FIELD(column_name, 'value1', 'value2', 'value3')";
$result = mysqli_query($connection, $query);

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