-1
$sqlQuery = "SELECT username as display_name, fullname as full_name, email as email, role as administrators, status as be_on FROM admin";

record role:0,1,...

I want the role of the administrators will display 1 is admin and if equal to 0 , then show the user.

Who can help me?

ArK
  • 20,698
  • 67
  • 109
  • 136

2 Answers2

0

I think your look for the IF statement

SELECT IF(`admin`.`role`= 1,TRUE,FALSE) FROM ...

http://www.mysqltutorial.org/mysql-if-function.aspx

CiaranSynnott
  • 908
  • 5
  • 9
0

My suggestion would be to use mysqli functionality as such:

<?php
$mysqliClass = mysqli_init();
$mysqliClass->real_connect("hostname or IP", "dbUsername", "dbPassword", "databasename");

$query = "SELECT `username` AS display_name, `fullname` AS full_name, `email`, `role` as administrators, `status` as be_on FROM `admin`";
$rs = $mysqliClass->query($query); # Preform your query
$showUser = false; # Start negative
$row = []; # Set a variable for the row to go into
while ($result = mysqli_fetch_assoc($rs)) # Fetch each result one at a time
{
    if ($result['administrators'] == 1) # Check the role
    {
        $showUser = true; # Set the show to true if it equals 1
        $row = $result; # Set the row to equal the result given
    }
    break; # You only need the first result
}
if ($showUser)
{
    /* Do your work with $row! */
}
?>

I have also removed email AS email for just email as it will already have this name so it is unrequired mess.

I would also recommend adding a WHERE clause in your query to reduce (filter) the results

If you are using an image from your DB, you could do something such as this:

<img src="<?=$row['image_column_name'];?>" height="120" width="120" />

And this should show the image as you want it to :)

Source of example code

Fuller example like the original answer:

<?php

$mysqliClass = mysqli_init();
$mysqliClass->real_connect("hostname or IP", "dbUsername", "dbPassword", "databasename");

$query = "SELECT `name`, `logo` AS describe , IF(`status` = 0, 'Online', 'Offline') as be_on FROM `domain`";
$rs = $mysqliClass->query($query);

$rows = []; # This will have more than one in it this time!
while ($result = mysqli_fetch_assoc($rs))
{
    $rows[] = $result;
}

foreach ($rows as $r)
{
    foreach ($r as $rowKey => $rowValue) # Double loop because you have an array in an array this way
    {
        if ($rowKey == "describe")
        {
            print '<img src="' . $rowValue . '" height="120" width="120" />';
        }
    }
}

?>
Community
  • 1
  • 1
Can O' Spam
  • 2,718
  • 4
  • 19
  • 45