Home > Mobile >  Multiple Mysql queries is not working in php
Multiple Mysql queries is not working in php

Time:10-13

I am trying to make an order history page for my project. The data is stored in the database table called order_today which holds the ids of the food ordered. But I need to display the name of the food which is stored in another table called menu and combo. I am only getting the result for one entry in the order_today table. the loop is not running. can anybody help? PS: I am a newbie in php.

<?php require 'config.php' ?>

<div >
    <div >
        Your Order History:
        <br>
        <?php
        session_start();
        $sql = "select * from order_today where `user_id`='" . $_SESSION['uid'] ."';";
        $result = mysqli_query($conn, $sql);
        while ($row = $result->fetch_assoc()) {
            $date = explode(' ', $row['time']);
            $combo = unserialize($row['combo_id']);
            $food = unserialize($row['food_id']);

        ?>
            <div >
                <div >
                    <h5><b>Order ID:<?php echo $row['Order_id']; ?></b></h5>
                    <div >
                        <h6> Date:<?php echo $date[0]; ?><br>Time:<?php echo $date[1]; ?></h6>
                    </div>
                    <div >
                        <?php
                        if (!empty($food['food'])) {
                            foreach ($food['food'] as $item => $itemQuantity) {
                                $sql1 = "select * from menu where `food_id`='" . $item . "';";
                                $result = mysqli_query($conn, $sql1);
                                while ($row1 = $result->fetch_assoc()) {
                                    echo $row1['name'] . ' x' . $itemQuantity . '<br>';
                                }
                            }
                        }
                        if (!empty($combo['combo'])) {
                            foreach ($combo['combo'] as $item => $itemQuantity) {
                                $sql2 = "select * from combo where `combo_id`='" . $item . "';";
                                $result = mysqli_query($conn, $sql2);
                                while ($row2 = $result->fetch_assoc()) {
                                    echo $row2['name'] . '(Combo) x' . $itemQuantity . '<br>';
                                }
                            }
                        }
                        ?>
                    </div>
                    <div >
                        <h5>Cost: </h5>
                    </div>
                </div>
            </div>
        <?php } ;?>


    </div>
</div>

CodePudding user response:

  1. session_start(); should be used before any HTML/headers output.
  2. Check $_SESSION['uid'] if it's good by printing the $sql query.
  3. Copy and paste the printed SQL and then run it in MySQL to see what it returns.

Overall, the while loop looks good, the problem could be with your stored data or $_SESSION['uid'] value because of incorrect using of session_start() function.

CodePudding user response:

I solved the query. For all 3 queries I simply used different result variable of the mysqli_query() function and now it works perfectly as needed. PS: Thanks for the response!

  •  Tags:  
  • html
  • Related