Search code examples
mysqlmysql-num-rows

mysql query run only for numbers


    $username = $_POST['username'];
    $password = $_POST['password'];
    $passworda = $_POST['passworda'];
    $email = $_POST['email'];

    if ($password == $passworda){
        echo 'this is the user name to be chacked:' .$username .'<br>';
        $query = "SELECT username FROM mishta where username=$username";
        $queryrun = mysql_query($query);
        echo $queryrun .'<br>';
        echo mysql_num_rows($queryrun) .'<br>';

        if (mysql_num_rows($queryrun)!=1){
            mysql_query("INSERT INTO mishta(id, username, password, email) VALUES ('', '$username','$password','$email')");
        }   
        else{
            echo 'user already exist';
        }

Hello there,

I'm trying to make a simple registration form, I get useername from html form and I want to make sure it doesn't already exist in my database. the thing is that whenever I run the username as a number (1,2,3...) it runs smoothly.

but, when I try to write anything with letter, like Bob or Dora I get the following error:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in...

Yet the information is still being stored my my data base, means it got false on the last if statement, and it will continue to get false (It will just insert duplicates if I try again).

Any help would be appreciated.


Solution

  • You forgot the quotes around the username:

    $query = "SELECT username FROM mishta where username='$username'"
    

    Quotes are used as string delimiter. Numbers are not string and it works if you use a number.