PaulB3434 PaulB3434 - 5 months ago 17
SQL Question

PHP/SQL - Selecting from ALL of a column where another column = 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]] . "\n";


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

Ali Ali
Answer

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 . "\n";
   }

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.
Comments