在PHP中用于MySQL查询的while()循环内的随机跳转

I am doing an example project for University and got a problem that I can't solve. In general, the project is to create an automated pizza order system in PHP and MySQL on Apache. The system works through the following steps: - Customer places order -> Baker receives order, proceeds -> Driver receives order at certain state, proceeds - Customer can view order at all time through session

Now I hung up at the last step: The driver can see a page that has a table with the information that the baker worked with and passed on (all changes are on database side). The driver can only see a whole package (whenever all pizzas are marked as a certain status, also saved in DB).

For this, I have the following SQL statement

SELECT PizzaID, BestellungID, Adresse, PizzaName, Preis, Status FROM angebot, bestelltepizza, bestellung where bestellung.bestellungid = bestelltepizza.fbestellungid and angebot.PizzaName = bestelltepizza.fPizzaName and (select min(status) from bestelltepizza where bestellung.bestellungid = fbestellungid) >2 ORDER BY Status, BestellungID

Now, when I use var_dump() to get the mysqli_num_rows() output, I get no errors and the following output int 26. Compared to the database rows, it's the correct number. I fetch the sql:

while($row = mysqli_fetch_array($this->result)) {
    var_dump(mysqli_num_rows($this->result));
    var_dump($row);
    ...
}

Within the while() loop contains another query

$this->query = "SELECT fPizzaName FROM bestelltepizza WHERE fBestellungID = '$BestellID'";
var_dump($this->query);
$tmpResult = $this->_database->query($this->query);
$count = mysqli_num_rows($tmpResult);

Now here is the problem, the while() loop leaves out a random $BestellID which can contain x rows of data. But when I count the output of var_dump() everything is correct. However, var_dump($this->query); is not showing the query statement for the specific jump, too.

Any ideas what this could be? Full link to pastebin below.

To not extend this question to the fullest, I uploaded the whole code to pastebin here: http://pastebin.com/u888CPLw

Offtopic: Appreciate any help, thanks. If I failed clearing out my exact problem or if any questions pop up to my question, please comment and I will clarify. Thanks.

                    while($row = mysqli_fetch_array($this->result)) { 
                        $count = mysqli_num_rows($tmpResult);

                        for($i = 0; $i < $count; $i++) {

                                $tmpVar = mysqli_fetch_array($this->result);

Ive snipped the code to show the problem

$count is based on $tmpResult you are then doing a fetch array on $this->result you should be doing it on $tmpResult

As Marc B says, Its a simple query to either inner join / left join on to the query. It would be better to use the join.