Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php mysqli_fetch_数组&;行希望参数1是mysqli_结果_Php_Sql_Mysqli - Fatal编程技术网

Php mysqli_fetch_数组&;行希望参数1是mysqli_结果

Php mysqli_fetch_数组&;行希望参数1是mysqli_结果,php,sql,mysqli,Php,Sql,Mysqli,我是SQL新手,我正在尝试建立一个登录系统。我遵循了指南,但在尝试登录时收到以下两条消息: 警告:mysqli_fetch_array()希望参数1是mysqli_结果,布尔值在第13行的C:\xampp\htdocs\loginguide/login.php中给出 警告:mysqli_num_rows()要求参数1为mysqli_结果,布尔值在第16行的C:\xampp\htdocs\loginguide\login.php中给出 我在MyPHPAdmin上创建了一个数据库,但是,我也不知道如

我是SQL新手,我正在尝试建立一个登录系统。我遵循了指南,但在尝试登录时收到以下两条消息:

警告:mysqli_fetch_array()希望参数1是mysqli_结果,布尔值在第13行的C:\xampp\htdocs\loginguide/login.php中给出

警告:mysqli_num_rows()要求参数1为mysqli_结果,布尔值在第16行的C:\xampp\htdocs\loginguide\login.php中给出

我在MyPHPAdmin上创建了一个数据库,但是,我也不知道如何正确地执行该操作,我是不是要向该数据库添加表

下面是login.php代码:

<?php
   include("config.php");
   session_start();

   if($_SERVER["REQUEST_METHOD"] == "POST") {
      // username and password sent from form 

      $myusername = mysqli_real_escape_string($db,$_POST['username']);
      $mypassword = mysqli_real_escape_string($db,$_POST['password']); 

      $sql = "SELECT id FROM admin WHERE username = '$myusername' and passcode = '$mypassword'";
      $result = mysqli_query($db,$sql);
      $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
      $active = $row['active'];

      $count = mysqli_num_rows($result);

      // If result matched $myusername and $mypassword, table row must be 1 row

      if($count == 1) {
         session_register("myusername");
         $_SESSION['login_user'] = $myusername;

         header("location: welcome.php");
      }else {
         $error = "Your Login Name or Password is invalid";
      }
   }
?>
<html>

   <head>
      <title>Login Page</title>

      <style type = "text/css">
         body {
            font-family:Arial, Helvetica, sans-serif;
            font-size:14px;
         }

         label {
            font-weight:bold;
            width:100px;
            font-size:14px;
         }

         .box {
            border:#666666 solid 1px;
         }
      </style>

   </head>

   <body bgcolor = "#FFFFFF">

      <div align = "center">
         <div style = "width:300px; border: solid 1px #333333; " align = "left">
            <div style = "background-color:#333333; color:#FFFFFF; padding:3px;"><b>Login</b></div>

            <div style = "margin:30px">

               <form action = "" method = "post">
                  <label>UserName  :</label><input type = "text" name = "username" class = "box"/><br /><br />
                  <label>Password  :</label><input type = "password" name = "password" class = "box" /><br/><br />
                  <input type = "submit" value = " Submit "/><br />
               </form>

               <div style="font-size:11px;<?php echo $error; ?></div>

            </div>

         </div>

      </div>

   </body>
</html>

登录页面
身体{
字体系列:Arial、Helvetica、无衬线字体;
字体大小:14px;
}
标签{
字体大小:粗体;
宽度:100px;
字体大小:14px;
}
.盒子{
边框:#666666实心1px;
}
登录
用户名:

密码:



您仅选择id,并使用“活动”

所以你应该先解决这个问题:

SELECT * FROM admin WHERE username = '$myusername' and passcode = '$mypassword'
它现在应该可以工作了

1)您需要在SQL查询中包含列“active”,或者用*

select * from admin where ....
(or)
select id, active from admin where ....
2) 检查给定示例中包含的config.php文件,如果DB_用户名、DB_密码、DB_数据库名称出现错误(语法错误),则可能会收到该警告


我认为您的查询失败并返回了一个错误的值。检查您的查询,如果它工作正常,请将其放入代码中:

$result = mysqli_query($db,$sql);

if (!$result) {
    printf("Error: %s\n", mysqli_error($db));
    exit();
}
了解更多信息

可能存在的副本
$result = mysqli_query($db,$sql);

if (!$result) {
    printf("Error: %s\n", mysqli_error($db));
    exit();
}