【问题标题】:PDO results are emptyPDO 结果为空
【发布时间】:2016-08-30 10:11:39
【问题描述】:

我决定从 mysqli/mysqlnd 切换到 PDO,但是我遇到了我上次这样做时遇到的问题。我再次尝试此操作,因为据称 PDO 似乎支持将包含值数组的变量传递给 execute() 参数以绑定到查询,而不必使用 call_user_func_array 之类的东西。

我的演示代码是:

$bind_arguments[] = "dogs";
$bind_arguments[] = "cats";
$bind_arguments[] = "birds";

$db = new PDO('mysql:dbname=' . SQL_DATA . ';host=' . SQL_SERVER, SQL_USERNAME, SQL_PASSWORD, array (
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
)); 

$sql = 'SELECT `name` FROM `pets` WHERE `type` = ? OR `type` = ? OR `type` = ?';

$result = Array();
try {

    if($stmt = $db->prepare($sql)) {

        $stmt->execute($bind_arguments);
        $result = $stmt->fetchAll();


    }
} catch(PDOException $e) {
    echo 'Wrong SQL: ' . $sql . ' Error: ' . $e->getMessage(); exit;
}

$db = null;

var_export($result);  // null

我没有遇到任何异常,但是 $result 为空。如果我使用 Navicat(或使用 mysqli)进行常规查询,它可以工作!

请参阅Example #5,这表明我应该能够做到这一点(从此处发布示例以供参考):

<?php
/* Execute a prepared statement using an array of values for an IN clause */
$params = array(1, 21, 63, 171);
/* Create a string for the parameter placeholders filled to the number of params */
$place_holders = implode(',', array_fill(0, count($params), '?'));

/*
    This prepares the statement with enough unnamed placeholders for every value
    in our $params array. The values of the $params array are then bound to the
    placeholders in the prepared statement when the statement is executed.
    This is not the same thing as using PDOStatement::bindParam() since this
    requires a reference to the variable. PDOStatement::execute() only binds
    by value instead.
*/
$sth = $dbh->prepare("SELECT id, name FROM contacts WHERE id IN ($place_holders)");
$sth->execute($params);
?>

为方便起见,另请参阅下面发布的Example #1

<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();

/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll();
print_r($result);
?>

那么为什么这不起作用?我做错了什么?

更新

在为StackOverflow's MVCE 要求发布我的代码(从一个更大的类中删除)时,我犯了一些拼写错误。这些错别字在原始课程中不存在。我已经在上面的代码中更新了它们。 - 很抱歉这可能造成的任何混乱。

【问题讨论】:

  • 在您发布的第一个代码 sn-p 中,您使用了 $db = new PDO...,但在 try/catch 块中您使用的是 $this-&gt;_db-&gt;prepare。那是你的真实代码还是?另外,你为什么要使用$db = null?这将完全消除任何性能问题,因为在 FCGI 应用程序中,您希望拥有持久连接以避免对每个请求进行 tcp 握手和凭据检查。
  • @Mjh - 好眼力,抱歉,我从 MVCE 的课程中​​收获了它。在实际项目中$this-&gt;_db是对象。更新上面的示例
  • 在某个时候,我遇到了类似的现象,并开始使用 bindValue 来设置值并在没有参数的情况下调用执行。 ;o(
  • 看起来您的问题是错误报告。 null 表示错误,但你什么也得不到。您的错误报告级别是多少?
  • @RyanVincent - 事实证明,我正在从一个没有返回任何结果的查询中寻找结果。这是怎么发生的——发生了很多事情,当我打开调试以查看发生了什么时(var_export() + exit;),它在我正在积极检查 XD 之前正在处理的查询之后终止。提示:这是人类需要睡眠的一个很好的理由,哈哈。

标签: php mysql mysqli pdo


【解决方案1】:

您正在为$bind_array$bnid_array 分配值,但正在将$bind_arguments 发送到execute()。尝试将$bnid_array 更改为$bind_array 并使用$stmt-&gt;execute($bind_array);

【讨论】:

  • 只是在为MVCE 进行更正时出现的错字。问题与此无关。
  • 另外,如果这是问题所在,它会在尝试执行空变量时产生 PDOException。
猜你喜欢
  • 2015-08-26
  • 2013-09-27
  • 2018-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-01
  • 1970-01-01
  • 2012-12-22
相关资源
最近更新 更多