doutangshuan6473 2015-03-11 14:05
浏览 37
已采纳

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

  • 写回答

1条回答 默认 最新

  • dongxi8993 2015-03-11 14:21
    关注
                        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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计