0

How do you get ALL the values from SQL columns if an 'if' statement is correct? Here is an example by what I mean:

TABLE:

|__ID__|__MadeBy__|__Username__|__Completed__|
|  1   |   user1  |  user14    |   TrueYes   |
|  2   |   user7  |  user14    |   TrueYes   |
|  3   |   NONE   |  user18    |   FalseNo   |
|  4   |   user25 |  user1234  |   TrueYes   |
|  5   |   NONE   |  user1234  |   FalseNo   |

What I want is to loop through the 'Completed' column and if there is a value, where it says FalseNo, then print other values from the SAME row, but not from all columns.

Example:

If Completed value = FalseNo, then print ID and Username from the same row.

Since there are more rows containing FalseNo values in the Completed column, display them also.

Example:

The program prints:

ID: 3
Username: user18

ID: 5
Username: user1234

What I tried doing is this:

<?php
//Coinflips Table
#Select the completed column
$selectCompleted = $mysqli->query("SELECT * FROM coinflips WHERE completed='FalseNo'");
#Get the row associated with the value
$getJoinableList = $selectCompleted->fetch_array(MYSQLI_ASSOC);
#Get the row's value of a column
$getJoinableBetCompleted = $getJoinableList['completed'];
$getJoinableBetID = $getJoinableList['id'];
$getJoinableBetUsername = $getJoinableList['username'];
if($getJoinableBetCompleted == "FalseNo") {
    echo "ID:" . $getJoinableBetID;
    echo "Username" . $getJoinableBetUsername;
}
?>

But this just crashes the website.

Thank you in advance, Laurynas :)

  • Aside: why are you using strings that say "FalseNo" and "TrueYes"? If you're trying to model Boolean data [a BIT or TINYINT(1) is a much better option than any string type](https://stackoverflow.com/a/289759/354577). – ChrisGPT was on strike Apr 15 '18 at 19:07
  • I'm still trying to understand this question. Are you asking how to loop through all the rows where `completed` is set to `"FalseNo"`? – ChrisGPT was on strike Apr 15 '18 at 19:14

2 Answers2

0

Two things:

You need to loop through your result set with a foreach - you are trying to access the row, and you're actually accessing the whole result set. If there's only one row you can get away with ...[0], but sounds like you are after multiple rows. Example:

foreach ($resultSet as $row) {
  ... now you can access the individual row & its columns, e.g. $row['column-name'] ... 
}

And check your column names, /including case/ - I get caught out by that every time.

MandyShaw
  • 1,088
  • 3
  • 14
  • 22
0

As Laurynas said, you have to loop for each row in your query to do what you want to do.

You can run the following code that will work:

# Create your select query
$query = "SELECT * FROM coinflips WHERE completed='FalseNo'";

# Execute your query
$resultset = $mysqli->query($query);

# Check if an error ocurred
if (!$resultset) {
    die("Error: " . $mysqli->error);
}

foreach ($resultset as $user) {

    //$completed = $user['completed'];
    $id = $user['id'];
    $username = $user['username'];

    # Removed because your query is doing this already
    // if ($completed == "FalseNo") {
        echo "ID: " . $id;
        echo "<br>";
        echo "Username: " . $username;
        echo "<br>";
    // }

}