-4

i am making a user login system through php i made the database made the connection made the form taking user name and password and storing in the variable

/// database connection code///

<?php
 mysql_connect('localhost','root','#########'); 
 mysql_select_db('scholarships_system');
 ?>

/////form data into variable/////

 if(empty ($_POST)=== false){
$username = $_POST['username'];
$password = $_POST['password'];
if(empty($username)=== true || empty($password)=== true )
{
$errors[] = 'You need to enter a username and password';
}
else if (user_exists($username)=== false)
{$errors[] = 'we cant find that username have you registerd ?';
}
}

/////// function code ////

function user_exists($username){
return (mysql_result(mysql_query(" SELECT COUNT (`user_id`) FROM `users_login` WHERE `username` = '$username'"),0) == 1) ? true : false;
    }

///if i call the function to check error occurs////

Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\wamp\www\login and registry\core\functions\users.php on line 6

////line 6 is////

return (mysql_result(mysql_query(" SELECT COUNT (`user_id`) FROM `users_login` WHERE `username` = '$username'"),0) == 1) ? true : false;
        }
Fluffeh
  • 33,228
  • 16
  • 67
  • 80
  • possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – deceze Sep 05 '13 at 11:05
  • NEVER post your root passwords - or actual passwords of any other manner for that matter in public sites. Edited to hide, but still visible when folks view revisions. before you do anything else, go change your password. – Fluffeh Sep 05 '13 at 11:06
  • On a side note: you should also be using mysqli or PDO, see here for details: http://www.php.net/manual/en/mysqlinfo.api.choosing.php – Joe Sep 05 '13 at 11:06
  • 1
    @Joe Nope. Count the braces again. The boolean conversion happens well after the `mysql_result`. :) – Carsten Sep 05 '13 at 11:08
  • 1
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Sep 05 '13 at 11:15

1 Answers1

1

you need to use like:-

function user_exists($username) {
   $count = mysql_result(mysql_query(" SELECT COUNT (`user_id`) FROM `users_login` WHERE `username` = '$username'"),0);
   return $count == 1 ? true : false;
}
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44