首页 > 解决方案 > PDO 不会执行,但在 phpmyadmin 中可以正常工作

问题描述

我有以下代码:

<?php
// ... other code...
$prepared_statement = <<< MYSQL
SELECT `Organization`, `City`, `Region`, `Country`, `Contact`, `Manager`, `Description`
FROM `businesses`
WHERE (
  (`Organization` LIKE ? OR `City` LIKE ? OR `Region` LIKE ? OR `Country` LIKE ? OR `Manager` LIKE ? OR `Description` LIKE ?)
  AND 
  (`Organization` LIKE ? OR `City` LIKE ? OR `Region` LIKE ? OR `Country` LIKE ? OR `Source` LIKE ? OR `Description` LIKE ?)
)
ORDER BY `Timestamp`;
MYSQL;
$value_list = ["Missouri","Missouri","Missouri","Missouri","Missouri","Missouri","US","US","US","US","US","US"]; // Populated through some passed in parameters, simplified to a known result here for simplicity sake.
try {
  $query = $this_pdo -> prepare();
  $query->execute($value_list); // <-- This line is where it breaks, but doesn't trigger the catch and doesn't output any error. Doing it in phpmyadmin returns 1 result.
  $result = $query->fetchAll(PDO::FETCH_ASSOC);
  return $result;
} catch (PDOException $e) {
  return ["A database problem has occurred: " . $e->getMessage()];
}

我试过直接在数据库中运行查询,替换“?” 用数组中的值标记,它可以工作。但执行不?

标签: phppdo

解决方案


发现了我的问题。简单的错误......忘记将我准备好的语句放入准备调用中。

*pdo->prepare()如果它为空,则不会返回错误,尽管它是空的,但后续执行调用会失败。*


推荐阅读