【问题标题】:Statement returns no records despite the parameter values provided in execute尽管执行中提供了参数值,但语句不返回任何记录
【发布时间】:2019-01-11 20:13:25
【问题描述】:

在我的下拉列表中使用 vardump 时,我收到 bool(false) 错误。

这是我的 SQL:

SELECT w_Continent.ID, w_Continent.NAME as continent_name, w_Country.Name, w_Country.Continent 
FROM w_Continent 
JOIN w_Country ON w_Continent.ID = w_Country.Continent 
GROUP BY w_Continent.ID

这是其他相关的 PHP 代码:

if (isset($_GET['country'])) {
    $contQuery = "
    {$sqlQ}
    WHERE w_Continent.ID = ID
    ";

    $continent = $db->prepare($contQuery);

    $continent->execute(['ID' => $_GET['country']]);

    $selectedCont = $continent->fetch(PDO::FETCH_ASSOC);

    var_dump($selectedCont);
}

【问题讨论】:

标签: php pdo prepared-statement


【解决方案1】:

占位符以冒号 : 为前缀。您的查询需要有 :ID 才能将其用作占位符并进行绑定。

: 告诉 PDO 它是一个命名占位符,而不是查询的其他有效部分,如列名。

【讨论】:

  • {$sqlQ} WHERE w_Continent.ID:ID "; 像这样?
  • WHERE w_Continent.ID=:ID
  • 另外,如果你的初始查询有那个 group by,它会再次失败,因为 group by 需要在 where 子句之后。
  • 它需要按 ID 分组以避免在动态填充的下拉列表中重复。
  • 我不是说不能使用 group by,而是需要在 where 子句后面加上 group by。
猜你喜欢
  • 2023-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-15
  • 2017-08-22
  • 2020-05-28
  • 2019-10-03
  • 1970-01-01
相关资源
最近更新 更多