How do I tell if the result of a mysql_query() is empty?

I’m trying to make a highly rudimentary login system, and part of that entails ensuring that when somebody tries to register a new username the name they want is not already taken.

Here’s what I have so far:

[php]
$user = htmlspecialchars($_POST[‘n’]);
$query = ‘SELECT UserID FROM UserList WHERE UserID = "’ . $user . ‘"’;
$result = mysql_query($query) or die('Query failed: ’ . mysql_error());
[/php]

Currently I’m trying to find a way to determine if there are no elements in $result, because that would mean that the name is not yet registered. Here’s how I’m doing it:

[php]
if(mysql_fetch_array($result, MYSQL_ASSOC) == false){
echo “result is null, name is free”;
}
else{
echo “result is not null, name is taken”;
}
[/php]

This is failing miserably for reasons beyond my comprehension (even when there are elements in $result, it always evaluates to false). Is there a better way?

Try to use something like the followin:

if ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
{    
echo "result is not null, name is taken"; 
}else{
echo "result is null, name is free"; 
}

Yep, everything works now. Thank you!

Sponsor our Newsletter | Privacy Policy | Terms of Service