diff --git a/email_registration.module b/email_registration.module
index 4f8546f..0c196af 100644
--- a/email_registration.module
+++ b/email_registration.module
@@ -11,6 +11,8 @@
  *
  */
 function email_registration_user($op, &$edit, &$account, $category = NULL) {
+  global $db_type;
+
   switch ($op) {
     case 'insert':
       // Other modules may implement hook_email_registration_name($edit, $account)
@@ -25,9 +27,18 @@ function email_registration_user($op, &$edit, &$account, $category = NULL) {
         $namenew = preg_replace('/@.*$/', '', $edit['mail']);
         // Remove unwanted characters
         $namenew = preg_replace('/[^a-zA-Z0-9.-]/', '', $namenew);
-
+        switch($db_type) {
+          // MySQL is case insensitive by default.  Removing LOWER allows the use of indexes.
+          case 'mysql':
+          case 'mysqli':
+            $sql = "SELECT count(*) FROM {users} WHERE uid <> %d AND name = '%s'";
+            break;
+          default:
+            $sql = "SELECT count(*) FROM {users} WHERE uid <> %d AND LOWER(name) = LOWER('%s')";
+            break;
+        }
         // if username generated from email record already exists, append underscore and number eg:(chris_123)
-        if (db_result(db_query("SELECT count(*) FROM {users} WHERE uid <> %d AND LOWER(name) = LOWER('%s')", $account->uid, $namenew)) > 0) {
+        if (db_result(db_query($sql, $account->uid, $namenew)) > 0) {
           // find the next number available to append to the name
           $sql = "SELECT SUBSTRING_INDEX(name,'_',-1) FROM {users} WHERE name REGEXP '%s' ORDER BY CAST(SUBSTRING_INDEX(name,'_',-1) AS UNSIGNED) DESC LIMIT 1";
           $nameidx = db_result(db_query($sql, '^'. $namenew .'_[0-9]+$'));
@@ -132,8 +143,19 @@ function custom_email_registration_name_submit($form, &$form_state) {
  *
  */
 function email_registration_user_login_validate($form, &$form_state) {
+  global $db_type;
   if (isset($form_state['values']['name'])) {
-    if ($name = db_result(db_query("SELECT name FROM {users} WHERE LOWER(mail) = LOWER('%s')", $form_state['values']['name']))) {
+    switch($db_type) {
+      // MySQL is case insensitive by default.  Removing LOWER allows the use of indexes.
+      case 'mysql':
+      case 'mysqli':
+        $sql = "SELECT name FROM {users} WHERE mail = '%s'";
+        break;
+      default:
+        $sql = "SELECT name FROM {users} WHERE LOWER(mail) = LOWER('%s')";
+        break;
+    }
+    if ($name = db_result(db_query($sql, $form_state['values']['name']))) {
       $form_state['values']['name'] = $name;
     }
   }
