php check username 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 if username already exists in php

$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$email = $_POST["email"];
$pass = $_POST["password"];

$check_email = mysqli_query($conn, "SELECT Email FROM crud where Email = '$email' ");
if(mysqli_num_rows($check_email) > 0){
    echo('Email Already exists');
}
else{
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $result = mysqli_query($conn, "INSERT INTO crud (Firstname, Lastname, Email, Password) VALUES ('$firstname', '$lastname', '$email', '$pass')");
}
    echo('Record Entered Successfully');
}

Tags:

Php Example