PHP MYSQLI while loop gets only 1 row

899

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.

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


            while($row1=mysqli_fetch_array($result1)){
                $t = $row1['time_id'];
        ?>  
            <tr><td>
            <a href=""><?php echo $t; ?></a>
            </td></tr>

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

<?php
           //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));;    

            while($row11=mysqli_fetch_array($result11)){

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

            <?php } ?>

What is wrong with my code in the appointment table?

970

Answer

Solution:

Classic

 $ids = [];
 while($row1=mysqli_fetch_array($result1)){
    $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

Source

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.