You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

点击按钮加载数据时触发mysqli_fetch_array参数错误,寻求帮助

解决mysqli_fetch_array参数错误的问题

嘿,我看到你遇到的这个错误了,咱们一步步拆解问题并修复它~

错误根源分析

你看到的Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result错误,主要来自三个核心问题:

  • 混用数据库扩展:你用mysqli_connect建立连接,却混用了PDO的prepare/bindParam/fetchAll方法,最后又想用mysqli_fetch_array处理PDO返回的数组,这两种扩展完全不兼容。
  • AJAX参数格式错误:你构建的dataString = ':id='+ id不是有效的POST参数格式,后端无法正确获取$_POST['id']。
  • 重复ID问题:表格里每一行的隐藏input都用了同一个id="text_id",JS只会获取到第一行的ID值,点击其他行也不会正确传递对应ID。

修正后的完整代码

1. HTML部分(修复重复ID问题)

<table>
  <tr>
    <th width="15%;">Image</th>
    <th width="0">ID</th>
    <th width="50%;">Name</th>
    <th>Action</th>
  </tr>
  <?php while ($row = mysqli_fetch_array($accounts)) { ?>
  <tr>
    <td><img src="images/<?php echo $row['image']; ?>"></td>
    <td><input type="hidden" class="text_id" value="<?php echo $row['id']; ?>"></td>
    <td><?php echo $row['lastname']; ?>, <?php echo $row['firstname']; ?></td>
    <td>
      <i class="fab fa-reddit-alien showdatacontainer" onclick="return chk(this)"></i>
      <i class="far fa-edit" style="margin: 0 7% 0 7%;"></i>
      <i class="fas fa-trash"></i>
    </td>
  </tr>
  <?php } ?>
</table>
<div id="datacontainer"></div>

这里把重复的id="text_id"改成class="text_id",并给点击的图标传递this参数,方便定位到当前行的ID。

2. 脚本部分(修复AJAX参数格式)

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
function chk(el) {
  // 通过当前元素找到所在行的隐藏ID
  var id = $(el).closest('tr').find('.text_id').val();
  // 使用标准的POST参数格式
  var dataString = {id: id};
  $.ajax({
    type: "post",
    url: "showdata.php",
    data: dataString,
    cache: false,
    success: function(data){
      $("#datacontainer").html(data);
      $("#datacontainer").slideToggle(300);
    },
    // 新增错误回调方便调试
    error: function(xhr, status, error) {
        console.log("请求出错: " + error);
    }
  });
  return false;
}
</script>

这里用jQuery的DOM遍历方法精准获取当前行的ID,同时用对象格式传递参数,确保后端能正确接收。

3. PHP处理部分(统一使用mysqli扩展,修复查询逻辑)

<?php
$db = mysqli_connect('localhost', 'root', '', 'psbr');
// 先检查数据库连接是否成功
if (!$db) {
    die("数据库连接失败: " . mysqli_connect_error());
}

$output = '';
if (isset($_POST['id'])) {
    $id = mysqli_real_escape_string($db, $_POST['id']); // 防止SQL注入
    // 使用mysqli预处理语句执行查询
    $stmt = mysqli_prepare($db, "SELECT * FROM accounts where id = ?");
    mysqli_stmt_bind_param($stmt, "i", $id); // "i"表示参数为整数类型
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    
    $output .= '<table>
                  <tr>
                    <td class="w"><h1>Name</h1></td>
                  </tr>';
    // 用mysqli_fetch_assoc遍历结果集
    while ($row = mysqli_fetch_assoc($result)) {
        $output .= '<tr>
                      <td>' . $row['lastname'] . '</td>
                    </tr>';
    }
    $output .= '</table>';
    // 释放语句资源
    mysqli_stmt_close($stmt);
}
echo $output;
mysqli_close($db);
?>

这里统一使用mysqli扩展的预处理语句,既避免了SQL注入风险,也解决了扩展混用导致的类型不匹配问题,同时正确处理查询结果集。


额外提示

  • 始终优先使用预处理语句处理数据库查询,这是防止SQL注入的最佳实践。
  • HTML中ID必须是页面唯一标识,重复ID会导致DOM操作异常,改用class批量处理更合理。
  • 可以在AJAX中保留error回调,方便快速排查请求失败的原因。

内容的提问来源于stack exchange,提问作者Spencer Sanchez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:51:16