Anda dapat mencoba melakukan:
connect_db();
$check = mysql_query("SELECT 'User' validation
FROM school_users
WHERE username = '$username'
UNION ALL
SELECT 'Email'
FROM school_users
WHERE email = '$email'") or die(mysql_error());
$row = mysql_fetch_assoc($check);
if($row)
{
if ($row["validation"] == 'User') {
respond("error", "Sorry, the username ".$_POST['username']." is already in use. Please choose a different username.");}}
else if ($row["validation"] == 'Email') {
respond("error", "Sorry, the email ".$_POST['email']." is already in use. Please choose a different email.");}}
}
ATAU Anda bisa melakukannya secara terpisah...
//Validate UserName
connect_db();
$check = mysql_query("SELECT username FROM school_users WHERE username = '$username'") or die(mysql_error());
$check2 = mysql_num_rows($check);
if ($check2 != 0) {
respond("error", "Sorry, the username ".$_POST['username']." is already in use. Please choose a different username.");}
//Validate Email
connect_db();
$checkEmail = mysql_query("SELECT email FROM school_users WHERE email = '$email'") or die(mysql_error());
$checkEmail2 = mysql_num_rows($check);
if ($checkEmail2 != 0) {
respond("error", "Sorry, the email ".$_POST['email']." is already in use. Please choose a different email.");}
Selain itu, kode Anda rentan terhadap serangan SQL Injection dan Anda menggunakan fungsi php MySQL yang tidak digunakan lagi. Jika Anda ingin membuat kode Anda lebih baik dan tidak terlalu rentan, lihat tautan berikut:
Mengapa saya tidak menggunakan fungsi mysql_* di PHP?
Apa yang bisa saya gunakan selain fungsi mysql_?
Pernyataan yang Disiapkan dengan MySQLi