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.