PHP MYSQLI while loop gets only 1 row


I have two ID in the 'appointment' table which are 1 and 2(ID). The two ID from the appointment table are came from the 'time' table. The time table have this ID (1,2,3,4,5).

in this query, the while loop echo the two ID which are 1 and 2(ID) which is correct.

            //appointment table
            $query1 = "SELECT * FROM appointment WHERE date_app = '$date' AND status = 'Active'";
            $result1 = mysqli_query($conn,$query1) or die("Error: ".mysqli_error($conn));;  

                $t = $row1['time_id'];
            <a href=""><?php echo $t; ?></a>

            <?php } ?>

in this query, i need to get all the ID that are not equal to value of the above query. when i echo ,I am getting a result of 2,3,4,5(ID) which is supposed to be 3,4,5(ID) only.

           //time table
            $query11 = "SELECT * FROM time WHERE time_id != '$t' ORDER BY STR_TO_DATE(time_sched,'%h:%i%p')";
            $result11 = mysqli_query($conn,$query11) or die("Error: ".mysqli_error($conn));;    


            <a href=""><?php echo $row11['time_id']; ?></a>

            <?php } ?>

What is wrong with my code in the appointment table?





 $ids = [];
    $ids[] = $row1['time_id'];

 //..... other code
 }//end of while loop for clarity.

 $query11 = "SELECT * FROM time WHERE time_id NOT IN(".implode(',', $ids).") ORDER BY STR_TO_DATE(time_sched,'%h:%i%p')";

You are only using the last ID from the first loop. So you need to store those IDs in an array and then using them in the input for IN.

In other words the variable$t is overwritten on each iteration of the first loop. Then you use it in your second query, so it only retains the last value from the previous loop.

Make sense.

People are also looking for solutions to the problem: php - Google bots get 503 errors while indexing my website


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.