0

I don't know what's the problem guys. Please help. Here it gives an error like this:

mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\test\session1.php on line 8

<?php
    error_reporting(E_ALL);
    require 'db.php';
    $username = $_SESSION['username'];
    $sql = "SELECT isadmin FROM user WHERE username='$username'"; 

    $result = mysqli_query($con,$sql); 
    $admin = mysqli_fetch_array($result); 

    $_SESSION['admin'] = $admin['admin']; 
    if ($_SESSION['admin'] == 1) { 
        echo "Welcome Admin"; 
    }else{ 
        echo "Normal User"; 
    }
?>
Shreyash S Sarnayak
  • 2,309
  • 19
  • 23

3 Answers3

0

This is so because there is some error in your query and it is returning false.

Try checking if $username is not empty and column referred in the query is correct.

And your query building is not the correct way, instead there should be parameters passed in the query which will make it safe from sql injection

Refer this link for more info : parameters in MySQLi

Community
  • 1
  • 1
Ambrish Pathak
  • 3,813
  • 2
  • 15
  • 30
  • Damn man sorry,table name is users not user i fixed it but now Error Appears: Undefined index: admin in C:\xampp\htdocs\test\session1.php on line 8,i'm makeing first time admin check –  Jan 14 '17 at 13:29
  • 1
    @GiorgiMacharashvili try t debug if admin has initialized as an array or not , like var_dump($admin); – Mohd Sadiq Jan 14 '17 at 13:56
  • 1
    Try changing - $_SESSION['admin'] = $admin['isadmin']; @GiorgiMacharashvili – Ambrish Pathak Jan 14 '17 at 14:02
0

try the following for the admin problem

$row = mysqli_fetch_array($result, MYSQLI_ASSOC);

you need to define the mode if you want to access the array with ['admin'] then you need to tell the mysqli_fetch_aaray to return an associative array

see http://php.net/manual/en/mysqli-result.fetch-array.php

0

Look at the column you're selecting:

SELECT isadmin
       ^^^^^^^

and then you're trying to use/fetch the admin column.

$admin['admin']
        ^^^^^

One of those is incorrect and they need to match and is unclear as to which column name is correct, isadmin or admin.

The most likely would be:

$admin['isadmin']

After seeing the error message you posted: (which I might add, that this answer was posted before seeing it).

Undefined index: admin

and basing myself on what you posted in your question.

If the admin column does (also) exist, then you need to add it in the SELECT, yet that doesn't seem to be the case here.

Basically, your query failed and you need to check for errors on the query.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141