简体   繁体   中英

How can I retrieve multiple data from DB through if statements?

I have a questionnaire which adds an 1 or a 0 to my DB. If the value is 0 this means the questions' answer was yes, if 1 the answer was no. what I need to do is run an if statement whereby the data for multiple statements are provided. some how the code only execute one "if statement" and I need it to execute multiple statement and return data associated with the relevant variable.

I have tried the following code '

    <?php
$results=$db->query("SELECT *
        FROM
            client_scope 
        WHERE
            client_scope_id = $siteID AND client_Id=
            '{$_SESSION['clientId']}'");
    $condition1= 1;
    $condition2= 4;

    foreach($results as $row){
        echo '<p> test';
        // if condition1 in db has been set and value equals to 0 echo data 
    if(isset($row['column_1'])){
        // echo $_SESSION['projectName'];
        if($row['column_1'] == 0){
            foreach($results1 as $row){
            echo '<tr>
            <td
            >'.$row['sectionName'].'</td>
            <td
            >'.$row['sectionRef'].'</td>
            <td
            >'.$row['requirement'].'</td>';
                } 
            }
        } 

      // if condition2 in db has been set and value equals to 0 echo data

    if(isset($row['column_2'])){
        if($row['column_2'] == 0){
            echo 'step in';
            foreach($results2 as $row){
            echo '<tr>
            <td
            >'.$row['sectionName'].'</td>
            <td
            >'.$row['sectionRef'].'</td>
            <td
            >'.$row['requirement'].'</td>';
                } 
            } 
        }
     }

     ?>

'

You are never using $condition1 and $condition2 after you give them their static values, which might be the problem here.

Another thing I would consider is switching from your current $result assignment to this:

$sql = "SELECT * FROM client_scope WHERE client_scope_id = '$siteID' 
        AND client_Id='".$_SESSION['clientId']."'";
$result = $conn->query($sql) or die($conn->error);

In your current query, you didn't include single quotes around $siteId , which was likely one of the problems because I had the same issue with a query yesterday.

Another problem was you had a set of single quotes inside another set of single quotes with your client_Id assignment, which doesn't work. Using the period operator, this method joins the $_SESSION part of the query with the rest. This is explained here .

Finally, the or die($conn->error) part of $result typically shows you errors that may exist like this in your queries in the future. For an explanation of this, see here .

This may be a personal preference, but I have always seen your foreach loop written as something similar to this:

while ($row = $result->fetch_assoc()) {
  echo json_encode($row);
}

I don't know if there is any functional difference, but it may be another factor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM