Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.