Gebruik de WHERE
clausule om alleen rijen met de opgegeven gebruikersnaam te krijgen:
"SELECT * FROM user_list WHERE uname='".$server->real_escape_string($uname)."'"
Controleer vervolgens of de zoekopdracht resulteert in het selecteren van rijen (ofwel 0 of 1 rij) met 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!";
}
}