在 php 显示中选择


Select in php display

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

记录角色:0,1,...

我希望管理员的角色将显示 1 是管理员,如果等于 0,则显示用户。

谁能帮助我?

我认为你寻找 IF 语句

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

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

我的建议是使用mysqli这样的功能:

<?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! */
}
?>

我还删除了email AS email只是为了email,因为它已经有了这个名字,所以它是不必要的混乱。

我还建议在查询中添加一个WHERE子句以减少(过滤(结果

如果您使用的是数据库中的映像,则可以执行以下操作:

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

应该显示您希望的图像:)

示例代码的来源

更完整的示例,如原始答案:

<?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" />';
        }
    }
}
?>