【问题标题】:Select in php display在php显示中选择
【发布时间】:2016-02-18 19:57:30
【问题描述】:
$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 是 admin ,如果等于 0 ,则显示用户。

谁能帮帮我?

【问题讨论】:

  • 您的实际需求是什么?
  • 执行查询后使用if条件
  • 我想选择以管理员身份检索记录角色中的值。但是当它显示在网络上时,如果它的角色 = 0 是管理员,角色 = 1 是用户。
  • 两个角色你都想要求?
  • 已发布答案

标签: php select


【解决方案1】:

我认为你对 IF 语句的看法

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

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

【讨论】:

    【解决方案2】:

    我的建议是使用 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 emailemail,因为它已经有了这个名称,所以它是不必要的混乱。

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

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

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

    而这个应该按照你想要的方式显示图像:)

    Source of example code

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

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

    【讨论】:

    • 如果它解决了您的问题,请考虑将其标记为正确并点赞:)
    • @TrầnAnhTuấn,当然,如果您可以使用您尝试过的一些代码为您的问题添加编辑,我可以看到我能做什么:)
    • 我在数据库中有 1 个记录标志。我想选择它以显示在我的网络中。
    • 我将对我的问题进行修改:)
    • @TrầnAnhTuấn 如果它解决了您的问题,请考虑接受答案。以下是meta.stackexchange.com/questions/5234/… 然后返回此处并对勾号/复选标记执行相同操作直到它变为绿色的方法。这会通知社区,找到了解决方案。否则,其他人可能会认为问题仍然悬而未决,可能想要发布(更多)答案。
    猜你喜欢
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 2014-08-24
    • 2020-04-25
    • 2019-02-13
    • 1970-01-01
    • 2016-12-06
    相关资源
    最近更新 更多