Gunakan WHERE
klausa untuk mendapatkan hanya baris dengan nama pengguna yang diberikan:
"SELECT * FROM user_list WHERE uname='".$server->real_escape_string($uname)."'"
Kemudian periksa apakah kueri menghasilkan pemilihan baris (baik 0 atau 1 baris) dengan MySQLi_Result::num_rows
:
function createUser($uname,$pword) {
$server->connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
$result = $server->query("SELECT * FROM user_list WHERE uname='".$server->real_escape_string($uname)."'");
if ($result->num_rows() === 0) {
if ($server->query("INSERT INTO user_list (uname) VALUES ('".$server->real_escape_string($uname)."'")) {
echo "User added Successfully";
} else {
echo "Error while adding user!";
}
} else {
echo "User already exists!";
}
}