【问题标题】:Warning: Invalid argument in php警告:php 中的参数无效
【发布时间】:2019-01-23 09:03:25
【问题描述】:

此文件为 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;
     ?>

【问题讨论】:

  • mysqli_num_rows() 返回结果集中的行数。

标签: php jquery mysqli


【解决方案1】:

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

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

}

【讨论】:

  • 如何解决这个问题 - 产品名称、类型、价格、品牌都未定义...在上面的代码中
【解决方案2】:

你犯的错误是你在foreach中使用了num_rows函数。我总是喜欢使用带有 fetch 的 while 循环。

while ($row = mysqli_fetch_assoc($result)) {
            $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>';
        }

【讨论】:

    【解决方案3】:

    mysqli_fetch_array 是您问题的解决方案。 foreach 需要一个数组来迭代它,之前的方法也提供了相同的功能。

    【讨论】:

      【解决方案4】:

      mysqli_fetch_assoc 将返回行作为关联数组,如果您想要完整的行作为关联数组,您必须使用mysqli_fetch_all($res, MYSQLI_ASSOC);

      【讨论】:

      • 请在此处添加示例及其说明,方便大家使用
      • 产品名称、类型、价格、品牌都是未定义的错误发生
      • 使用 while ($row = mysqli_fetch_assoc($result)) { 而不是 foreach (mysqli_num_rows($result) as $row) {
      • 如果你想使用 foreach 那么它应该是这样的:- $result = mysqli_fetch_all($result, MYSQLI_ASSOC); foreach ($result as $row) {
      • @JituRawat 您为 foreach 提出的解决方案虽然改变了逻辑并获取了所有内容。如果你想使用 foreach 你想使用迭代器,例如foreach($mysqli-&gt;query('SELECT x from y') as $row) { }
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多