What are the best practices for handling case sensitivity in SQL queries when using MySQL or MariaDB in PHP?

When working with MySQL or MariaDB in PHP, it is important to handle case sensitivity properly in SQL queries to avoid unexpected results. One way to address this issue is by using the COLLATE keyword in your queries to specify the desired case sensitivity for string comparisons.

// Example SQL query with case-insensitive comparison
$query = "SELECT * FROM users WHERE username = 'john' COLLATE utf8_general_ci";
$result = mysqli_query($connection, $query);

// Process the query result
if ($result) {
    while ($row = mysqli_fetch_assoc($result)) {
        // Handle the retrieved data
    }
} else {
    // Handle query error
}