Jump to content

if username taken


jimmyt1988

Recommended Posts

my code is:

<?php
    $usernameFetch = mysql_query("select username from userRegistration where ($username==username)");
    if ($usernameFetch){
        echo "Username already taken";
    }
    else{    
        mysql_query("insert into userInformation(firstName, lastName, day, month, year, country, emailAddress, username, password) 
            values('$firstName','$lastName','$day','$month','$year','$country','$emailAddress','$username','$convertedPassword')");
    }
?>

 

I want it to say if the mysql query returns true then it should echo "already taken". However it doesn't. I'm completely missing something basic :(

Link to comment
https://forums.phpfreaks.com/topic/184439-if-username-taken/
Share on other sites

mysql_query() for a SELECT query returns either a FALSE value or a result resource. The FALSE value is returned if the query failed to execute. This is usually due to syntax errors or problems with the database. A result resource will be returned if the query executed without any errors. A query that matches zero rows in your table is a successful query and it returns a result resource. You must use mysql_num_rows to find out how many rows there are in the result set that is returned.

Link to comment
https://forums.phpfreaks.com/topic/184439-if-username-taken/#findComment-973621
Share on other sites

<?php
    $usernameFetch = mysql_query("select username from userRegistration where ($username==username)");
    $usernameValidation = mysql_num_rows($usernameFetch);
    
    if ($usernameValidation>0){
        echo "Username already taken";
    }
    else{    
        mysql_query("insert into userInformation(firstName, lastName, day, month, year, country, emailAddress, username, password) 
            values('$firstName','$lastName','$day','$month','$year','$country','$emailAddress','$username','$convertedPassword')");
    }
?>

 

Hm now what's wrong?

 

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in D:\****\index.php on line 17

Link to comment
https://forums.phpfreaks.com/topic/184439-if-username-taken/#findComment-973663
Share on other sites

You are getting a FALSE value from your mysql_query() because the query is failing.

 

You need single-quotes around the $username variable in the query so that it will be a string value and you need to use only one = sign for the comparison in the query.

Link to comment
https://forums.phpfreaks.com/topic/184439-if-username-taken/#findComment-973667
Share on other sites

 $usernameFetch = mysql_query("select username from userRegistration where username = '$username'") or trigger_error('Query Failed: ' . mysql_error());

 

You had an error in your Query, the above should work, for future testing I would suggest to use the trigger_error function so you can see errors as shown above.

Link to comment
https://forums.phpfreaks.com/topic/184439-if-username-taken/#findComment-973668
Share on other sites

 $usernameFetch = mysql_query("select username from userRegistration where username = '$username'") or trigger_error('Query Failed: ' . mysql_error());

 

You had an error in your Query, the above should work, for future testing I would suggest to use the trigger_error function so you can see errors as shown above.

 

Add the or trigger_error part to your code as shown above and see what error MySQL is throwing so you can accurately fix it.

Link to comment
https://forums.phpfreaks.com/topic/184439-if-username-taken/#findComment-973675
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.