0

** SOLVED **

** I made a stupid typo in the function sanitize. I created her as sanitze. Thank you **

If anybody could help me, I would appreciate it very much. I am learning the database interaction and have a User in my database called 'George'.

To check if he exists I have this function:

function sanitze($data) {
    return mysql_real_escape_string($data);
}

function user_exists($username) {
    $username = sanitize($username);
    $query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'");
    // Check the result of the query (COUNT) and return either true or false
    return (mysql_result($query, 0) == 1) ? true : false;
}

And then I am checking the Username with this If statement:

if(user_exists('George') === true) {
    echo 'User exists';
} else {
    echo 'User doesn\'t exist';
} die ();

But I don't get any results. I found this Thread: PHP username check function not working

And I am currently working on it, but haven't found any solution yet.

If anyone knows the answer. It would be great.

8
  • Do you have a database connection? Commented Nov 24, 2013 at 14:35
  • Why count? instead of SELECT username FROM users where name = ? ?? Commented Nov 24, 2013 at 14:35
  • Check out PDO prepared statements. Even with your sanitize function, this approach still isn't safe. Commented Nov 24, 2013 at 14:37
  • @Qǝuoɯᴉs Why get the username if you don't want it? The username is already the input for the query. Commented Nov 24, 2013 at 14:38
  • write as well. -> ....WHERE username = '" . $username . "'" Commented Nov 24, 2013 at 14:38

1 Answer 1

2

The function is called sanitze and you call it using sanitize. That's two different spellings.

If you enable error reporting (either in runtime or in your configuration), PHP should show you pretty detailed information about errors like this:

Sign up to request clarification or add additional context in comments.

2 Comments

I hope this isn't the issue :)
That happens. If you're new to some specific thing (like databases), you automatically assume that that is where the error must be. One good reason to use error reporting, so you're not in the blind.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.