【问题标题】:PHP PDO Select query not selecting rows from databasePHP PDO选择查询未从数据库中选择行
【发布时间】:2014-01-03 13:43:15
【问题描述】:

我正在运行这个 PDO 查询:

$stmt = $pdo_conn->prepare("SELECT * from billing_control where sequence = :sequence ");
$stmt->execute(array(':sequence' => $_GET["sequence"]));
$result = $stmt->fetch();

从数据库中选择行,但是当我执行 var_dump($smtm);我得到了这个结果:

object(PDOStatement)#2 (1) { ["queryString"]=> string(57) "SELECT * from billing_control where sequence = :sequence " }

我的 URL 末尾有 ?sequence=178,所以它应该运行 SQL:

select * from billing_control where sequence = 178

有什么想法我做错了吗?

【问题讨论】:

  • 你连接到数据库了吗?
  • 您是否检查了 $_GET["sequence"] 的值,或者是否定义了 $_GET["sequence"] ?
  • 我相信无法检索使用默认 PDO 类插入的参数的查询字符串,除非您自己进行自定义。您应该检查数据库中收到的查询。还要确保在绑定(或在 bindParam 方法中)之前将参数转换为正确的类型

标签: php mysql sql pdo


【解决方案1】:

试试:

$stmt = $pdo_conn->prepare("SELECT * from billing_control where sequence = :sequence ");
$stmt->bindParam(':sequence', $_GET["sequence"])
$stmt->execute();

另一个版本是:

$stmt = $pdo_conn->prepare("SELECT * from billing_control where sequence = ? ");
$stmt->execute(array("%$_GET[sequence]%"));

【讨论】:

  • 我在我的问题中输入的代码适用于其他 SELECT 查询,是否有任何原因它不起作用?
  • 这些查询是否还包括将变量绑定到 GET 参数?
  • 我不确定,我问题中的代码是我必须选择行的唯一代码
  • 我刚刚设置了 $sequence = $_GET["sequence"];然后 $stmt->execute(array(':sequence' => $sequence));
  • 我需要为所有东西创建变量吗?
【解决方案2】:

试试这个

$query = $pdo_conn->prepare("SELECT * from billing_control where sequence = :sequence ");
$query->bindParam(':sequence', $_GET["sequence"], PDO::PARAM_STR, 255); //I assume that sequence data is string
$result = $query->execute();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    相关资源
    最近更新 更多