查询返回空结果但数据存在

Posted

技术标签:

【中文标题】查询返回空结果但数据存在【英文标题】:Query return empty result but data exist 【发布时间】:2018-06-19 13:37:10 【问题描述】:

我使用SlimPDOmysql 来返回我的数据库中可用的matches 的特定列表。我的查询是这样的:

SELECT m.*, 
       t.name AS home_team_name, 
       t2.name AS away_team_name 
FROM `match` m 
LEFT JOIN team t ON m.home_team_id = t.id 
LEFT JOIN team t2 ON m.away_team_id = t2.id
WHERE (home_team_id = 117 OR away_team_id = 117) AND round_id = 488

如果我执行这个query,我会得到matches的列表:

但在使用Slim 开发的API 内部,我得到一个空数组。这是方法结构:

$app->get('/match/get_matches_by_team/round_id/team_id/type', function (Request $request, Response $response, array $args)

  $query = "SELECT m.*,
  t.name AS home_team_name,
  t2.name AS away_team_name
  FROM `match` m
  LEFT JOIN team t ON m.home_team_id = t.id
  LEFT JOIN team t2 ON m.away_team_id = t2.id
  WHERE ";

  switch($args["type"])
  
    case "home":
          $query .= "home_team_id = :team_id AND ";
    break;
    case "away":
        $query .= "away_team_id = :team_id AND ";
    break;
    default:
        $query .= "(home_team_id = :team_id OR away_team_id = :team_id) AND ";
    break;
  

  $query .= "round_id = :round_id";

  $sql = $this->db->prepare($query);
  $sql->bindParam("team_id", $args["team_id"]);
  $sql->bindParam("round_id", $args["round_id"]);
  $sql->execute();

  $result = $sql->fetchAll();
  return $response->withJson($result);
);

我做错了什么?

提前感谢您的帮助。

更新

如果我这样做echo $query; return;,我会得到:

SELECT m.*,
t.name AS home_team_name,
  t2.name AS away_team_name
  FROM `match` m
  LEFT JOIN team t ON m.home_team_id = t.id
  LEFT JOIN team t2 ON m.away_team_id = t2.id
  WHERE away_team_id = :team_id AND round_id = :round_id

假设通过away,如果我通过all,我会得到:

SELECT m.*,
t.name AS home_team_name,
  t2.name AS away_team_name
  FROM `match` m
  LEFT JOIN team t ON m.home_team_id = t.id
  LEFT JOIN team t2 ON m.away_team_id = t2.id
  WHERE (home_team_id = :team_id OR away_team_id = :team_id) AND round_id = :round_id

更新 2

使用建议的提示更新方法

$app->get('/match/get_matches_by_team
/round_id/team_id/type', function (Request $request, Response $response, array $args)

  $query = "SELECT m.*,
  t.name AS home_team_name,
  t2.name AS away_team_name
  FROM `match` m
  LEFT JOIN team t ON m.home_team_id = t.id
  LEFT JOIN team t2 ON m.away_team_id = t2.id
  WHERE ";

switch($args["type"])
   
    case "home":
          $query .= "home_team_id = :home_team_id 
    AND ";
        break;
        case "away":
            $query .= "away_team_id = :away_team_id AND ";
        break;
        default:
            $query .= "(home_team_id = :home_team_id OR away_team_id = :away_team_id) AND ";
        break;
      

      $query .= "round_id = :round_id";

      $sql = $this->db->prepare($query);
      $sql->bindParam("home_team_id", $args["team_id"]);
      $sql->bindParam("away_team_id", $args["team_id"]);
      $sql->bindParam("round_id", $args["round_id"]);
      $sql->execute();

      $result = $sql->fetchAll();
      return $response->withJson($result);
    );

【问题讨论】:

打印出你正在执行的查询。 @GordonLinoff 请检查我的更新 。 .你能让 any 带有命名参数的查询起作用吗?我的意思是,只需执行select :param 并查看该值是否按预期返回。 @GordonLinoff 如果我只做SELECT * FROM :param 工作 【参考方案1】:

你的:

 $sql->bindParam("team_id", $args["team_id"]);
 $sql->bindParam("round_id", $args["round_id"]);

试试这个,参数可能需要不同的格式

$sql->bindParam(":team_id", $args["team_id"], PDO::PARAM_INT);
$sql->bindParam(":round_id", $args["round_id"], PDO::PARAM_INT);

$sql->bindParam(":team_id", $args["team_id"]);
$sql->bindParam(":round_id", $args["round_id"]);

【讨论】:

@popop 嗯好吧让我再看一遍【参考方案2】:

选择默认开关时,您尝试使用相同的参数标记(在您的情况下:team_id)绑定值。为了让它工作,你必须在 PDO 中打开仿真模式。

http://www.php.net/manual/en/pdo.prepare.php

当您调用 PDOStatement::execute() 时,您必须为希望传递给语句的每个值包含一个唯一的参数标记。除非打开了仿真模式,否则您不能在准备好的语句中多次使用同名的命名参数标记。

【讨论】:

您所做的现在将在其他 2 种情况下引发错误,因为绑定的值与您的参数标记不同。您要么必须使用以前的方法打开仿真模式,要么运行相同的开关并仅绑定相应的变量。此外,您还必须在 bindParam 通话中添加 : 我在您的代码中没有看到任何 PDO 错误报告机制 :) 我在 slim 配置中声明了错误报告,并且到目前为止按预期工作,无论如何,感谢您的帮助

以上是关于查询返回空结果但数据存在的主要内容,如果未能解决你的问题,请参考以下文章

thinkphp-查询数据-基本查询

MySQL 数据库查询返回空结果

查询同一张表时,spark sql 返回空值,但配置单元和 impaly 获取正常数据?

如何保持电源查询的空返回值?

pymssql 不返回结果数据

Javers 查询返回空结果