doucan8521 2016-06-05 15:19
浏览 31
已采纳

PHP / SQL - 从另一列= 0的所有列中选择

I'm trying to get this statement to select all of the column called "Roles" where Status = 0. With the code I have it is only picking the first one with status = 0. I feel stupid asking but how could I select all roles where status = 0? Current Code:

    $sql_query="SELECT Role FROM roles Where Status=0";
    $result = mysqli_query($dbconfig,$sql_query);
    $input = mysqli_fetch_array($result);
    $role = array_rand($input, 2);
    echo "<br>";
    echo "You are are a ".$input[$role[1]] . "
";

Note: $dbconfig is coming from a required file in a different location.

  • 写回答

1条回答 默认 最新

  • duanjue2560 2016-06-05 15:35
    关注

    Why are you using array_rand()

    array_rand — Pick one or more random entries out of an array

    http://php.net/manual/en/function.array-rand.php

    Try something like this:

        $sql_query="SELECT Role FROM roles Where Status=0";
        $result = mysqli_query($dbconfig,$sql_query);
        while($row = mysqli_fetch_array($result))
        {
            $role = $row[0];
            echo "<br>";
            echo "You are are a ".$role . "
    ";
       }
    

    Based on your comment, maybe this:

        $sql_query="SELECT Role FROM roles Where Status=0";
        $result = mysqli_query($dbconfig,$sql_query);
        $myRoleArray;
        while($row = mysqli_fetch_array($result))
        {
            $role = $row[0];
            $myRoleArray[] = $role;
        }
        //You can pick random from $myRoleArray now.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习残差模块模型
  • ¥20 两个不同Subnet的点对点连接
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)