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

PHP查询在phpMyAdmin中返回有效结果但JSON编码后所有值均为null的问题求助

问题排查与解决方案

这个问题的核心大概率是**bind_result的变量顺序/数量和SELECT语句返回的字段不匹配**,导致变量没有被正确赋值,最终数组里的内容都是null。咱们一步步来分析:

1. 定位问题根源

你提到循环里echo $link能输出正确值,但JSON里全是null,这里很可能有个误区:你echo的$link可能是之前代码残留的旧值,而不是fetch()从结果集中获取到的新值。因为当bind_result的变量数量和SELECT字段数量不一致时,MySQLi会无法正确绑定变量,fetch()时不会给这些变量赋值,它们会保持初始的null状态(除非之前有赋值)。

仔细看你的代码:

  • SELECT语句返回的字段是5个:".$table.".".$column.", ".$table.".".$condition_code.", id, ".$condition_degree.", univLinkImg.Link
  • 但bind_result却传了6个变量:$major,$university,$code,$id,$degree,$link

多出来的$code变量没有对应的字段,这会导致整个绑定逻辑错位,后面的变量都无法正确匹配到SELECT的字段,自然都是null。

另外,就算数量对了,如果变量顺序和SELECT的字段顺序不对应,也会出现赋值错误的情况(比如把id的值赋给了$code,把degree的值赋给了$id等)。

2. 快速修复方案

方案一:修正bind_result的变量匹配

首先,先打印出实际执行的SQL语句,确认字段顺序和数量:

echo $qu; // 把这条语句复制到phpMyAdmin里执行,看返回的字段顺序和数量

然后让bind_result的变量顺序、数量和SELECT的字段完全一致。比如如果SELECT返回的字段顺序是:major, uni_code, id, degree, link,那绑定应该是:

$stmt->bind_result($major, $uni_code, $id, $degree, $link);

之后调整$temp数组的赋值,让每个键对应正确的变量:

$temp["maj"] = $major;
$temp["univ"] = $uni_code; // 对应SELECT里的大学code字段
$temp["code"] = $id; // 对应SELECT里的id字段
$temp["id"] = $id; // 这里注意,你原来的$id对应SELECT的第四个字段,要根据实际情况调整
$temp["degree"] = $degree;
$temp["link"] = $link;

方案二:改用get_result()获取关联数组(更推荐)

bind_result很容易因为字段顺序出错,改用get_result()可以直接获取关联数组,避免顺序问题,代码也更直观:

mysqli_select_db($con,"websem_university") or die("could not connect database");
header('Content-Type: application/json; charset=utf-8');
mysqli_set_charset($con,"utf8");
$qu="select ".$table.".".$column.", ".$table.".".$condition_code.", id, ".$condition_degree.", univLinkImg.Link from ".$table.", univLinkImg where ".$condition_faculty."='".$faculty."' and ".$table.".".$condition_code."='".$uni."' and univLinkImg.code='".$uni."'";
$stmt=$con->prepare($qu);
$stmt->execute();
// 改用get_result获取结果集
$result = $stmt->get_result();
$majors=array();
while ($row = $result->fetch_assoc()) {
    $temp=array();
    // 直接用字段名(或别名)来取值,不用关心顺序
    $temp["maj"] = $row[$column];
    $temp["univ"] = $row[$condition_code];
    $temp["code"] = $row["id"];
    $temp["id"] = $row[$condition_degree];
    $temp["degree"] = $row[$condition_degree];
    $temp["link"] = $row["Link"];
    array_push($majors, $temp);
}
echo json_encode($majors, JSON_UNESCAPED_UNICODE);

这个方法的好处是,你可以直接通过字段名来获取值,完全不用考虑SELECT的字段顺序,大大降低出错概率。

3. 额外注意事项

  • 永远要检查SQL语句的正确性:执行前打印$qu,在phpMyAdmin里验证,确保返回的字段和你预期的一致。
  • 注意SQL注入风险:你的代码里直接把变量拼接到SQL语句中(比如'".$faculty."'),这存在严重的SQL注入风险,建议改用参数绑定:
// 示例:用?占位符代替变量,然后绑定参数
$qu="select ".$table.".".$column.", ".$table.".".$condition_code.", id, ".$condition_degree.", univLinkImg.Link from ".$table.", univLinkImg where ".$condition_faculty."=? and ".$table.".".$condition_code."=? and univLinkImg.code=?";
$stmt=$con->prepare($qu);
$stmt->bind_param("sss", $faculty, $uni, $uni); // 根据变量类型调整参数类型(s=字符串,i=整数等)
$stmt->execute();

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 17:52:42