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

MySQL 5.7环境下PHP fetch_assoc返回空值问题求助

React + PHP + MySQL 跨版本兼容性问题:fetch_assoc() 在 MySQL 5.7 中返回空值

环境差异

  • 本地环境:MySQL 8.2 + PHP 8.2.13,代码正常运行
  • 虚拟主机环境:MySQL 5.7 + PHP 8.2.13,fetch_assoc() 返回空值,fetch_all() 可正常工作

问题代码

$query = "SELECT * FROM `usuario` WHERE `login` = ?;";
$stmt = $conn->prepare($query);
$stmt->execute([$login]);

$result = $stmt->get_result();

$results = $result->fetch_assoc();

复现步骤

  • 安装带有 MySQL 5.7 插件的最新 Wamp Server
  • 创建名为'testes'的虚拟主机,指向测试仓库的'backend'文件夹
  • 执行 npm i 和 npm run start 启动 React 应用
  • 打开浏览器控制台,点击按钮触发请求

输出对比

MySQL 8.2 下的输出

post or get
line with data
rest of the code

MySQL 5.7 下的输出

post or get
rest of the code

说明:无任何错误抛出,返回结果完全为空(无null、无字符)

调试信息(var_dump 结果)

var_dump($stmt)

<pre class='xdebug-var-dump' dir='ltr'>
<small>C:\Leyed\REPO\testes\backend\index.php:76:</small>
<b>object</b>(<i>mysqli_stmt</i>)[<i>2</i>]
  <i>public</i> 'affected_rows' <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>1</font>
  <i>public</i> 'insert_id' <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>0</font>
  <i>public</i> 'num_rows' <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>0</font>
  <i>public</i> 'param_count' <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>1</font>
  <i>public</i> 'field_count' <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>14</font>
  <i>public</i> 'errno' <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>0</font>
  <i>public</i> 'error' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>''</font> <i>(length=0)</i>
  <i>public</i> 'error_list' <font color='#888a85'>=&gt;</font> 
    <b>array</b> <i>(size=0)</i>
      <i><font color='#888a85'>empty</font></i>
  <i>public</i> 'sqlstate' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'00000'</font> <i>(length=5)</i>
  <i>public</i> 'id' <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>1</font>
</pre>

var_dump($result)

<pre class='xdebug-var-dump' dir='ltr'>
<small>C:\Leyed\REPO\testes\backend\index.php:76:</small>
<b>object</b>(<i>mysqli_result</i>)[<i>3</i>]
  <i>public</i> 'current_field' <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>0</font>
  <i>public</i> 'field_count' <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>14</font>
  <i>public</i> 'lengths' <font color='#888a85'>=&gt;</font> <font color='#3465a4'>null</font>
  <i>public</i> 'num_rows' <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>1</font>
  <i>public</i> 'type' <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>0</font>
</pre>

var_dump($results)

<pre class='xdebug-var-dump' dir='ltr'>
<small>C:\Leyed\REPO\testes\backend\index.php:76:</small>
<b>array</b> <i>(size=14)</i>
  'ID' <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>2</font>
  'Login' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'leyed'</font> <i>(length=5)</i>
  'Senha' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'$2y$10$fm8FKZ2NfEO3YEkTmW8M6uHMIVCbhwvG8ICtg/hxJ1gSSCVWY5BAq'</font> <i>(length=60)</i>
  'Nome' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>''</font> <i>(length=0)</i>
  'CPF' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>''</font> <i>(length=0)</i>
  'Genero' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>''</font> <i>(length=0)</i>
  'Email' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'leyed94@gmail.com'</font> <i>(length=17)</i>
  'Telefone' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>''</font> <i>(length=0)</i>
  'Nascimento' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'1994-08-12'</font> <i>(length=10)</i>
  'Endere�o' <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>0</font>
  'Criado' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'2023-12-18 22:24:10'</font> <i>(length=19)</i>
  'UltLogin' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'2023-12-21 23:46:32'</font> <i>(length=19)</i>
  'Desde' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'2023-12-18 22:24:10'</font> <i>(length=19)</i>
  'Permisses' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>''</font> <i>(length=0)</i>
</pre>

补充说明

已开启PHP错误报告,无任何报错。目前计划在正常工作的MySQL 8.2环境中复现,对比找出差异点。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 14:27:03