Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm going through a vid tutorial on creating a user reg & login form in php and mysql.

The following script should echo 'exists' on the login.php form, however just shows a blank (indicating the user does not exist, when it does and s in fact the only username on the DB)

<?php
    function user_exists($username) {
   $username = sanitize($username);
   $query = mysql_query("SELECT * FROM `users` WHERE `username` = '$username'"); 
   return (mysql_result($query, 0) == 1) ? true : false;
 }
?>

The login form portion of code goes as follows:

if (user_exists('andy') === true) {
echo 'exists';
}
die();

Am I missing some syntax or something obvious?? here is a link to the vid tutorial if it helps http://www.youtube.com/watch?v=Til3oVNlho4

share|improve this question
What's your OS? Your BLANK screen - does it have any message on it? Perhaps you don't have a database connection open. Is there any other code on your page? – itsols 20 mins ago
2  
That serie of tutorials contains a lot of bad practice: from using deprecated functions like mysql_query to vulnerable concepts by using md5 for password hashing, and a lot more. If you want to do it right, then I recommend to read a good book. – HamZa 19 mins ago
What happens if you run the query by itself in a mysql tool instead of on the web with php? – Paul 19 mins ago
2  
Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the warning? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. – tereško 16 mins ago
@tereško Funny you should say that. The link for the tutorial you provided the OP is also mysql_, and not mysqli, so why keep the OP on the same track? ;) – Fred 13 mins ago
show 4 more comments

1 Answer

Why not do like this?

<?php
function user_exists($username) {
$username = sanitize($username);
$query = mysql_query("SELECT * FROM `users` WHERE `username` = '$username'"); 
$exists = mysql_num_rows($query) == 1 ? 1 :0;

return $exists; 
}
?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.