简体   繁体   中英

PHP/mySQLi : Loop inside a loop doesn't return all results

I'm basically in the process of designing a web front-end for a menu, I have created a system to add meals to the menu and now I need to read it to the user. I also need to organize things into categories (aka div's). To do so I wrote code that (I think) should get a list of categories (distinct results in a column), then get the names and prices of each meal for each category.

The issue is that it is not displaying all the categories or meals.

Here's my DB structure, ID is the primary index 数据库结构 My code looks like this,

        <?php
        mysqli_report(MYSQLI_REPORT_ALL ^ MYSQLI_REPORT_INDEX);
        $db = new mysqli('localhost', 'username', 'password', 'db');
        if($db->connect_errno > 0){
            die('Unable to connect to database [' . $db->connect_error . ']');
        }

        $query = "SELECT DISTINCT `mealtype` FROM `meals`";
        if(!$result = $db->query($query)){
            die('There was an error running the query [' . $db->error . ']');
            }
        else {
            $row = $result->fetch_assoc();
            while($row = $result->fetch_assoc()){
                $mealtype=$row['mealtype'];
                echo "<div class='pure-u-1-3'><h1>".strtoupper($mealtype)."</h1>";
                $mealquery = "SELECT * from `meals` where `mealtype` = '".$mealtype."'";
                $mealresult = $db->query($mealquery);
                $mealrow = $result->fetch_assoc();
                echo $db->error;
                while($mealrow = $mealresult->fetch_assoc()){
                    echo "<b>".$mealrow['name']."</b>".$mealrow['price'].'<br>';
                    }
                echo "</div>";

            }

        }


        $db->close();
?>

I also noted that without mysqli_report(MYSQLI_REPORT_ALL ^ MYSQLI_REPORT_INDEX); I get a warning, but to my understanding this is due to the use of the DISTINCT selector.

when Error reporting is fully activated it returns

PHP Fatal error:  Uncaught exception 'mysqli_sql_exception' with message 'No index used in query/prepared statement SELECT DISTINCT `mealtype` FROM `meals`' in (workingdir)/index.php:42

I'm at a complete loss as to why it won't display everything, I'm sure it's to do with the double loop, as when I just ask it to return the table in the menu manager it works fine.

EDIT : The Issue turned out to be a 1D13T error, I should have payed more attention to my code.

you have extra $row = $result->fetch_assoc(); in your code

while that error, everyone is so impressed with, is irrelevant to this problem.

by the way, the best setting for mysqli report is

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

I wish this kind of questions were legitimate and welcome, yet entitled to deletion right after being solved.

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