-2

My code is not working i want to check Email exist in db or show message email exit or not when click tab button:

HTML:

<input class="form-control" type="text"  name="email" id="email" onkeyup="checkemail();" required>```

PHP:

<?php
    $checkemail = mysqli_query($conn, "Select email FROM users WHERE email = '$email'")
    while(mysqli_fetch_assoc($checkemail) > 0)
         {
          echo "Email Already Exist :)";
         }
         else
         {
          echo "OK  :)";
         }
        exit();
        }
?>
user3783243
  • 5,368
  • 5
  • 22
  • 41
Khurram
  • 13
  • 5
  • 1
    `mysqli_fetch_assoc` doesn't return an integer. You should first prevent SQL injections, `Select count(*) FROM users WHERE email = ?` (and just get a count). Then execute, check the return and output based on that. – user3783243 Aug 25 '20 at 13:40
  • Use `mysqli_num_rows($checkemail) ` instead of `mysqli_fetch_assoc($checkemail)`. Try This – Vinayak Aug 25 '20 at 13:41
  • You need to check if it's `NULL`. See [here](https://www.php.net/manual/en/mysqli-result.fetch-assoc.php) for more. – Vasilis G. Aug 25 '20 at 13:41

2 Answers2

0

You need to check num_row like that:

 $checkemail = $conn->prepare("Select email FROM users WHERE email = ?");
 $checkemail->bind_param('s',$email);
 $checkmail->execute();
 $result = $checkmail->get_result();
 if ($result->num_rows > 0) {
   echo 'mail exist'; exit();
  }else{
  echo "mail doesn't exist";
  }

i change part of the code for improve security

Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34
-1

Use mysqli_num_rows($checkemail) instead of mysqli_fetch_assoc($checkemail)

Because mysqli_fetch_assoc($checkemail) does not return a integer. It returns an associative array.

$checkemail = mysqli_query($conn, "Select email FROM users WHERE email = '$email'")
        
         if(mysqli_num_rows($checkemail) > 0)
             {
              echo "Email Already Exist :)";
             }
             else
             {
              echo "OK  :)";
             }
            exit();
            }
        
        
        ?>
Vinayak
  • 80
  • 8