首页 > 解决方案 > 警告:php 中的参数无效

问题描述

此文件为 foreach 提供了错误无效参数。有没有人解决这个问题。我尝试了很多时间,但我找不到它,我不知道如何解决这个问题。这是我的代码:

 <?php
    include('connect.php'); 
    $query = "SELECT * FROM product";
    $result = mysqli_query($connect,$query);
    $output ='<table class="table table-stripped table-bordered">
    <tr><th>Product Name</th>
    <th>Product Type</th>
    <th>Product Rate</th>
    <th>Brand</th>
    <th>Edit</th>
    <th>Insert</th>
    </tr>';
    if(mysqli_num_rows($result)>0)
    {
        foreach (mysqli_num_rows($result) as $row) {
            $output .= '<tr>
                <td width="40%">'.$row["product_name"].'</td>
                <td width="40%">'.$row["product_type"].'</td>
                <td width="40%">'.$row["product_rate"].'</td>
                <td width="40%">'.$row["brand"].'</td>
                <td width="10%">
                    <button type="button" name="edit" class="btn btn-primary btn-xs edit" id="'.$row["id"].'">Edit</button>
                </td>
                <td width="10%">
                    <button type="button" name="delete" class="btn btn-danger btn-xs delete" id="'.$row["id"].'">Delete</button>
                </td>
            </tr>';
        }
    }

    else
    {
        $output .='<tr>
        <td colspan ="4" align="center"> No data Found</td>
        </tr>' ;
    }

    $output .= '</table>';
    echo $output;
     ?>

标签: phpjquerymysqli

解决方案


mysqli_num_rows函数返回一个数字,而不是一个数组。您可能想使用mysqli_fetch_all

foreach (mysqli_fetch_all($result) as $row) {

}

推荐阅读