一般来说,如何防止PHP中的Mysql_fetch_assoc错误? [重复]

Possible Duplicate:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in problema con el wile

When I execute login form , I'll encounter with the following error:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\login\check.php on line 22

  <?php

  include_once ("function.php");

  $username = $_POST['tfuser'] ;
  $password = $_POST['tfpass'] ;

  if (isset($username) && isset($password)){

    $link = mysql_connect('localhost','root','') or die ('error in connecting to db');
    mysql_select_db('login',$link) or die ('error select db');

    $sql = "select from * from administration 
    where username='$username' and password='$password'";

    $result = mysql_query($sql,$link);

    if (mysql_fetch_assoc($result)){
         //login to panel 
         redirect("panel.php");
    } else {
        //back to login page
    }

  } else {
      //back to login page
      redirect("index.php");
  }

  ?>

And now how can I prevent this error display and prevent Mysql_fetch_assoc error ?

Please read about SQL Injections.

You are receiving the following error because the execution of your SQL statement failed.

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\login\check.php on line 22

Your SQL query in:

$result = mysql_query($sql,$link);

returns FALSE. That is why when you use mysql_fetch_assoc() it returns a warning.

To fix this, ensure that your $result contains a mysql resource. One way you can do this is to stop script execution in case of a failed SQL query. You can do this:

$result = mysql_query($sql,$link) OR die(mysql_error());

That will show you why your SQL execution failed.

your mysql query is false which results shows error Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\login\check.php on line 22 you got this because you use wrong way $result = mysql_query($sql,$link);//incorrect methode $result = mysql_query($sql);//correct methode

this error occurred because query can not fetch any result.best way to handle these type error is echo query and see the output of query in browser screen.cut the query part and paste it to sql and run.sure it will returns error......