dongrongdao8902 2012-10-27 12:48
浏览 28
已采纳

一般来说,如何防止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 ?

  • 写回答

2条回答 默认 最新

  • dtlc84438 2012-10-27 12:55
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备