-1

In an application that I am building, I need to display different kinds of buttons for users with different designations (HOD, Director or Staff). Consider the code snippet-

        <button id="request">Leave Request</button>
        <button id="statistics">Leave Statistics</button>
        <?php
        $sql = "SELECT Role from employee where `Emp Code`=$emp_code";
        $result = mysqli_query($link,$sql);
        $row =mysqli_fetch_array($result, MYSQLI_NUM);
        if($row[0] == "HOD"){
            echo "<button id='new'> New Button</button>";
        }

So is the logged in used is a HOD then he/she should see 3 buttons.

Error displaying- Couldn't fetch query. Parameter 1 in mysqli_fetch_array should be mysqli_result, none given.

3 Answers3

3

As OP mentioned in comments the table structure is emp_name, emp_code, password, emailID, role - varchar(50) and $emp_code is string.

and the query OP is trying;

$sql = "SELECT Role from employee where `Emp Code`=$emp_code";

so query should be

$sql = "SELECT role FROM employee WHERE emp_code='$emp_code'";
Shehary
  • 9,926
  • 10
  • 42
  • 71
  • even though I use "SELECT Role from employee where `Emp Code`='aal01' ". aal01 is a value in 'Emp Code' column , it is showing the same error – Raghav Motwani Sep 17 '15 at 05:19
  • check the queries in answer, you have wrong table col names in query, if as you mentioned in comments that the table col is `emp_code` but in query you are trying with `Emp Code` – Shehary Sep 17 '15 at 05:20
  • and same goes with col `role` and you are trying `Role` – Shehary Sep 17 '15 at 05:22
  • 1
    there is no possible way the col name will be `Emp Code` can't put space between 2 words, so it's totally wrong – Shehary Sep 17 '15 at 05:23
  • @Shehary Not quite ^ ^ Although not considered good practice, spaces are allowed but ticks are required to be used around table/column names. However, it looks like the OP's column name is different than what they originally used. – Funk Forty Niner Sep 17 '15 at 12:56
  • @Fred-ii- you are right, actually i never saw and never tried to create a col having space between 2 words, always used `_` so i didn't know that it's possible and it can be done but just created a col with space between 2 words and yes I was Totally Wrong about my comment :) – Shehary Sep 17 '15 at 13:00
  • @Shehary Same thing for a hyphen `table or col-name`, allowed, but not good practice and also requires ticks around it. MySQL sees that as wanting to do math. I.e.: "col **minus** name". – Funk Forty Niner Sep 17 '15 at 13:02
  • 1
    @Fred-ii- yeah agree with you `-` hyphen is not practice also. space between 2 words in col name was something new to me, glad learned something. – Shehary Sep 17 '15 at 13:29
1

I'm on my phone but since emp_code is a string please try the following. Notice the ' ' around $emp_code

        <button id="request">Leave Request</button>
    <button id="statistics">Leave Statistics</button>
    <?php
    $sql = "SELECT Role from employee where `Emp Code`='$emp_code'";
    $result = mysqli_query($link,$sql);
    $row =mysqli_fetch_array($result, MYSQLI_NUM);
    if($row[0] == "HOD"){
        echo "<button id='new'> New Button</button>";
    }
Bart Scheffer
  • 497
  • 1
  • 3
  • 18
0

Please try this .

        <button id="request">Leave Request</button>
        <button id="statistics">Leave Statistics</button>
        <?php
        $sql = "SELECT `role` from employee where `emp_code`='".$emp_code."'";
        $result = mysqli_query($sql);
        $row =mysqli_fetch_array($result, MYSQLI_NUM);
        if($row[0] == "HOD"){
            echo "<button id='new'> New Button</button>";
        }
dhi_m
  • 1,235
  • 12
  • 21