PHP return true / false depending on number of rows.

I am trying to check if a username is already used in tblUsers, but for some reason it returns true, even though it should be false as the number of rows should come back as zero.

    $usernamequery = "SELECT COUNT(`userID`) FROM `tblUsers` WHERE `username` = '$username'";
    $query = mysqli_query($con, $usernamequery);
    $queryarray = mysqli_fetch_array($query, MYSQLI_BOTH);
    $queryarrayres = mysqli_num_rows($query);
    return ($queryarrayres > 0)?true:false;
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

take Count out of your query. You are returning 1 row with a count of 0.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
You don't need to remove COUNT() you can just do this
1. Give the COUNT() an alias - not necessary but easier if you do
2. you are calling mysqli_fetch_array - which gets an associative and 0 indexed array - this will help if you don't alias your COUNT() but it does have some side effects you should be aware of - especially when you iterate over the returned array - field values will be duplicated. I prefer fetch_object
3. Return the value of the count as your true / false value

$usernamequery = "SELECT COUNT(`userID`) AS TotalUsers FROM `tblUsers` WHERE `username` = '$username'";
$query = mysqli_query($con, $usernamequery);
$queryarray = mysqli_fetch_object($query);
// TotalUsers will be 0 if there are none - which is a falsy value - 
// everything else will be truthy so you can just return the value
return $queryarray->TotalUsers; 

Open in new window

By removing the COUNT() you are potentially compromising efficiency as your query could return more than one row (unlikely in this case - but in general something to think about) - you are then doing a fetch_array (which you don't use) and a num_rows() - so lots of spinning the CPU's wheels unnecessarily.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.