doulong1987 2015-08-22 14:03
浏览 51
已采纳

而在while循环似乎不起作用 - PHP

Alright so I've been trying to replace a number from the database for the actual name, unfortunately it only returns nothing at all.

I'm not sure what I'm doing wrong here, Tried it multiple ways, as explained on other related questions, yet it doesn't seem to work. The first while seems to work, but the second doesn't do its job..(although in this setup it gives nothing at all)

here is my code:

<?php
include_once 'db_connect.php';

$query  = "SELECT bestel_nummer, bestel_datum, bestel_tijd, kkt_id, mwr_id
        FROM bestellingen";

$response = mysqli_query($mysqli, $query);


if ($response){

    echo '<table align="left" cellspacing="5" cellpadding="8">
            <td align="left"><b>bestel nummer<b></td>
            <td align="left"><b>bestel datum<b></td>
            <td align="left"><b>bestel tijd<b></td>
            <td align="left"><b>klanten nummer<b></td>
            <td align="left"><b>Medewerker<b></td></tr>';


while($row = mysqli_fetch_array($response)){
    $mwr_id = $row['mwr_id'];
    $query2 = "SELECT 'voornaam' FROM 'medewerkers' WHERE 'mwr_id' = $mwr_id LIMIT 1";
    $response2 = mysqli_query($query2, $mysqli);
            while($row2 = mysqli_fetch_array($response2)){
            

            echo '<tr><td align="left">'. 
        $row['bestel_nummer'].'</td><td align="left">'.
        $row['bestel_datum'].'</td><td align="left">'.
        $row['bestel_tijd'].'</td><td align="left">'.
        $row['kkt_id'].'</td><td align="left">'.
        $row2['voornaam'].'</td><td align="left">';
        echo'</tr>';
            }
    }


echo'</table>';
}
else{
    echo 'couldnt issue database query';
    echo mysqli_error($mysqli);
}



mysqli_close($mysqli);
?>

Thanks in advance.

</div>
  • 写回答

1条回答 默认 最新

  • duanhongyi2964 2015-08-22 14:07
    关注

    Maybe it's not the while loop, but the mysqli_query Seems like the arguments in the second query have a wrong order

    first query

    $response = mysqli_query($mysqli, $query);
    

    second query

    $response2 = mysqli_query($query2, $mysqli);
    

    This means no result, no while loop, check the response2 variable, the while loop should work, but actually the code itself looks unperformant.

    If you wanna have the most performance you should do it with one query, with something like Group and Join. I don't know your structure but it should be possible.

    If you try to query the db, in a loop it's a alway a very bad idea, image 100, 1000 users will trigger this code.. ohoohooo your server will probably go down men.

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

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程