user2376313 user2376313 - 6 months ago 9
SQL Question

Checking if a query returns any information

I've been working on this for hours, can't seem to figure out why its not working.

All of the tutorials I've looked at showed that this worked fine. The output of

num_rows
is greater than
1
and yet it still goes inside the
if
statement.

Also it returns
1
even when it should be
0
. Maybe it's a
BOOL
?

if($test = mysqli_num_rows($result) == 0){
echo "Username and Email are Available test= $test";
} else {
echo "Username and Email are TAKEN";
}

Answer

Your statement only evaluates to true if num_rows is equal to one:

if($test = mysqli_num_rows($result) == 0){

You need to change it to more than equals to 1 or more than 0:

if(mysqli_num_rows($result) >= 1){
    echo "Username and Email are Available test= $test";
} else {
    echo "Username and Email are TAKEN";
}

Usage of mysqli_num_rows():

Returns the number of rows in the result set.