-1

Im having this error mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in line 121 when I try to login to the system. here is my code section for particular problem. Does anyone know why this is happen to the code ?

if(isset($_POST["loginbtn"]))
{
$uid = $_POST["userid"];
$pword = $_POST["userpass"];

$result = mysql_query("SELECT * FROM admin WHERE Ad_ID = '$uid' AND Ad_Pass = '$pword'
   ");
if($row = mysql_fetch_assoc($result))
{

    $_SESSION ["loggedin"] = "true" ;
   $_SESSION ["adid"] = $row ["Ad_ID"];
   $_SESSION ["adname"] = $row ["Ad_Name"];
   $_SESSION ["adaright"] = $row ["Ad_ARight"];

    if($row ["Ad_ARight"]=='write')
        header('Location: HomeScreen.php');
    else if($row ["Ad_ARight"]=='read')
        header('Location: HomeScreenr.php');

}
else
{    
       echo '<div class="alert-message error"><p><b> Invalid username or              
password </b></p></div>';
}
}
?>
<div align="center">
user2687998
  • 11
  • 1
  • 1

5 Answers5

1

your query has failed, so mysql_query has returned a boolean false value.

The best way to figure out what's wrong is to add a error handler.

$result = mysql_query("SELECT * FROM admin WHERE Ad_ID = '$uid' AND Ad_Pass = '$pword'") or die(mysql_error());
DevZer0
  • 13,433
  • 7
  • 27
  • 51
0

Execute the query with proper Ad_ID and Ad_Pass and then see if it displays any result.. Probably the result is blank.

SELECT * FROM admin WHERE Ad_ID = '$uid' AND Ad_Pass = '$pword'
Rakesh Shewale
  • 497
  • 6
  • 22
0

Pls DO NOT use mysql, use mysqli or PDO

Pls consult documentation, what does mysql_query() return when there is an error? You will probably find it useful to run mysql_error() for more detail.

Owen Beresford
  • 712
  • 4
  • 10
0

Try to check with mysql_num_rows otherwise check your query using mysql_error().

if(isset($_POST["loginbtn"]))
{
$uid = $_POST["userid"];
$pword = $_POST["userpass"];

$result = mysql_query("SELECT * FROM admin WHERE Ad_ID = '$uid' AND Ad_Pass = '$pword'
   ") die(mysql_error());
$num=mysql_num_rows($result);
if($num)
{
    $row = mysql_fetch_assoc($result)
    $_SESSION ["loggedin"] = "true" ;
   $_SESSION ["adid"] = $row ["Ad_ID"];
   $_SESSION ["adname"] = $row ["Ad_Name"];
   $_SESSION ["adaright"] = $row ["Ad_ARight"];

    if($row ["Ad_ARight"]=='write')
        header('Location: HomeScreen.php');
    else if($row ["Ad_ARight"]=='read')
        header('Location: HomeScreenr.php');

}
else
{    
       echo '<div class="alert-message error"><p><b> Invalid username or              
password </b></p></div>';
}
}
?>

Please check this code. I am not sure it is working or not. I think it will working perfectly.

Chinmay235
  • 3,236
  • 8
  • 62
  • 93
-1

Try doing the check like this. Check if $result was set and returns a value. Then assign the array to $row:

if(isset($result))
{

   $row = mysql_fetch_assoc($result)

   $_SESSION ["loggedin"] = "true" ;
   $_SESSION ["adid"] = $row ["Ad_ID"];
   $_SESSION ["adname"] = $row ["Ad_Name"];
   $_SESSION ["adaright"] = $row ["Ad_ARight"];

    if($row ["Ad_ARight"]=='write')
        header('Location: HomeScreen.php');
    else if($row ["Ad_ARight"]=='read')
   header('Location: HomeScreenr.php');

}

What you can also try is to check if it returns any rows:

$result = mysql_query("SELECT * FROM admin WHERE Ad_ID = '$uid' AND Ad_Pass = '$pword'");
$num_rows = mysql_num_rows($result);

if($num_rows > 0)
{
  $row = mysql_fetch_assoc($result)
   //CODE HERE
}
Conrad Lotz
  • 8,200
  • 3
  • 23
  • 27