我在哪里弄乱这个?不理解循环

时间:2017-08-08 18:08:49

标签: php sql loops if-statement

enter image description here

<td bgcolor="aabbcc"><center>Line 1</td>
 <?php
    //Connect to mysql server
    $con = mysqli_connect('localhost', 'User', 'Pass', 'Database');
    if (!$con) {
    die ("connection error". mysqli_connect_error());
    }

$sql = " SELECT * FROM PMAsset where LineNum ='1' ORDER BY LineNum, AssetNum";
    $result = mysqli_query($con, $sql) or die(mysqli_error($con));

    $count = 0;
    while($row = mysqli_fetch_array($results)) {
?>
        <td Bgcolor=<?php echo $row['PMStatus']?>><?php echo $row['AssetNum']?></td>;

        <?php
        $count++;
    }
    while ($count++ < 10) {
       echo '<td />';
}
           ?>

我要做的是根据SQL数据创建表格并填充单元格我现在得到的错误

  

[08-Aug-2017 13:55:01 America / New_York] PHP警告:mysqli_fetch_array()期望参数1为mysqli_result,在第42行给出为null

第41-43行:

    while($row = mysqli_fetch_array($results)) {
?>
<td Bgcolor=<?php echo $row['PMStatus']?>><?php echo $row['AssetNum']?></td>;

运行时,它应从数据库中提取并将td bgcolor更改为数据库中的内容,并在单元格中显示assetnum作为文本。如果没有记录,则td bgcolor应为黑色。对于其余列。我想我很接近,但我知道我也有点离开了。建议?

1 个答案:

答案 0 :(得分:2)

我可以在您的代码中看到两个问题:

1

while($row = mysqli_fetch_array($results)) {

需要

while($row = mysqli_fetch_assoc($result)) { // remove s from $results and use _assoc which makes your array more leighter

2

<td Bgcolor=<?php echo $row['PMStatus']?>><?php echo $row['AssetNum']?></td>;

需要:

<td bgcolor="<?php echo $row['PMStatus']?>"><?php echo $row['AssetNum']?></td>;
//It needs to be a small b in Bgcolor and quotes are missing around its value