php mysql check if username already exists code example

Example 1: php check if user exists in database

$q = mysql_query("SELECT username FROM User WHERE username=$username", $cnn); if (mysql_num_rows($q) != 0) { echo "Username exists"; } else { echo "Write your code here"; }

Example 2: how to check user already exists in php

$sql = "SELECT username FROM table_name WHERE username='{$username}'";
$result = mysqli_query($con,$sql) or die("Query unsuccessful") ;
      if (mysqli_num_rows($result) > 0) {
        echo "Username is already exist";
      } else {
             ...............   
      }

Tags:

Php Example