错误日志中的错误PHP警告:为foreach()提供的参数无效

时间:2017-07-22 15:37:26

标签: php error-handling

错误日志出错但结果显示完好我不知道错误文件中出现此错误的方式和原因。

PHP警告:为foreach()提供的参数无效请帮我解决这个问题怎么摆脱这个错误?

<?php

    $business_name = $value['business_name'];
    $sql = "SELECT * FROM listings_reviews WHERE business_name = '$business_name' order by listing_date DESC";
    $result = $conn -> query($sql);

    while ($row = $result->fetch_assoc()) {
      $data[] = $row;
    }

    //print_r($row); //exit;
    ?>


    <?php
    foreach ($data as $value){ ?>

    <div class="col-md-12 reviews">
    <span style="color:#000"><?php echo hide_mail ($value['reviewer_email'])?></span>&nbsp;&nbsp;
    <span style="color:#000">
    <?php

    $starNumber = $value['rating'];

    //echo $starNumber; exit;

        for($x=1;$x<=$starNumber;$x++) {
            echo '<i style="font-size:12px; color:#00BF9A" class="fa fa-star" aria-hidden="true"></i>';
        }
        if (strpos($starNumber,'.')) {
            echo '<i style="font-size:12px; color:#00BF9A" class="fa fa-star-half-o" aria-hidden="true"></i>';
            $x++;
        }
        while ($x<=5) {
            echo '<i style="font-size:12px; color:#00BF9A" class="fa fa-star-o" aria-hidden="true"></i>';
            $x++;
        }
    ?>
    </span> 

    <span style="float:right; color:#666; font-size:14px"><?php echo $value['listing_date']?></span>
    <hr style="margin:5px">
    <span style="color:#666"><?php echo $value['comment']?></span>





    </div>
    <?php
    }  

    ?>

1 个答案:

答案 0 :(得分:1)

在某些情况下,没有结果,因此永远不会调用$data[] = $row;,并且$data未定义。

修复方法是正确初始化$data

$data = []; // add this

while ($row = $result->fetch_assoc()) {
    $data[] = $row;
}