【问题标题】:Fatal Error: fetch()致命错误:fetch()
【发布时间】:2013-12-10 21:14:30
【问题描述】:

我有下面的 sn-p 代码。当我运行整个程序时,它在此部分失败并出现错误Fatal error: Call to a member function fetch() on a non-object in <filename> on line 26。我真的不确定我应该怎么做才能解决这个问题。如果这很重要,我会针对我的数据库运行 SQL 查询,它会准确返回它应该返回的内容。

if(isset($_POST['submit'])) {
        $query = 'SELECT email FROM users WHERE email=:email';
        $query_params = array(':email' => $_POST['email']);

        try {
            $stmt = $conn->prepare($query);
            $result = $stmt->execute($query_params);
        } catch(PDOException $ex) {
            echo $ex->getMessage();
        }

        $row = $result->fetch();   //fails on this line

        if(empty($results)) {
            $passset = 1;
        }
    }

【问题讨论】:

  • 虽然该链接有助于调试 PDO,但它根本不是这个问题的重复

标签: php mysql pdo fetch fatal-error


【解决方案1】:

PDOStatement::execute 返回 TRUE 或 FALSE,而不是结果对象。

改变这个:

$row = $result->fetch();

到这里:

$row = $stmt->fetch();

【讨论】:

  • 在中断 8 个月后回到 PHP;我一定会搞砸一些简单的事情。谢谢你,我很感激!
猜你喜欢
  • 2015-06-18
  • 1970-01-01
  • 1970-01-01
  • 2021-06-08
  • 2012-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-18
相关资源
最近更新 更多