-1

I'm new to SQL and I'm trying to set up a log-in system. I've followed a guide but I'm getting these 2 messages when I try to login:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\loginguide/login.php on line 13

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\loginguide\login.php on line 16

I created a database on MyPHPAdmin however, I don't know how to do that properly either, am I meant to add tables to that database?

Here's the login.php code:

<?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>
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
Rickie
  • 1
  • 3
  • Possible duplicate of [mysqli\_fetch\_array()/mysqli\_fetch\_assoc()/mysqli\_fetch\_row() expects parameter 1 to be resource or mysqli\_result, boolean given](https://stackoverflow.com/questions/2973202/mysqli-fetch-array-mysqli-fetch-assoc-mysqli-fetch-row-expects-parameter-1) – Rajdeep Paul Sep 17 '17 at 08:48

3 Answers3

1

1) You need to include column "active" in your SQL query or just replace 'id' with an *

select * from admin where ....
(or)
select id, active from admin where ....

2) Check your config.php file that you have included in the given example, you could get that warning if the DB_USERNAME, DB_PASSWORD, DB_DATABASE names are given wrong(grammatical mistakes).

<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'root');
define('DB_DATABASE', 'testDB');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>
Prashanth kumar
  • 949
  • 3
  • 10
  • 32
-1

I think your query is failing and returning a false value. Check your query and if it is working fine, put this in your code:

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

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

for more information.

http://www.php.net/manual/en/mysqli.error.php

Nikhil Parmar
  • 495
  • 3
  • 17
-2

You only select id, and you use "active"

So you should fix that first:

SELECT * FROM admin WHERE username = '$myusername' and passcode = '$mypassword'

It should work now

James Bob
  • 85
  • 1
  • 2
  • 10