php 在布尔值上调用成员函数 fetch()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32696192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:07:30  来源:igfitidea点击:

Call to a member function fetch() on boolean

phpbooleanfetch

提问by marjanos

I receive this error:

我收到此错误:

Fatal error: Call to a member function fetch() on boolean in C:\xampp\htdocs\repo\generator\model\database.php on line 34

致命错误:在第 34 行调用 C:\xampp\htdocs\repo\generator\model\database.php 中布尔值的成员函数 fetch()

When I run this code:

当我运行此代码时:

    class database
    {
        private $user = 'root';
        private $pass = '';
        public $pdo;

        public function connect() {
            try {
                $this->pdo = new PDO('mysql:host=localhost; dbname=generatordatabase', $this->user, $this->pass);
                echo 'Po??czenie nawi?zane!';
            }
            catch(PDOException $e) {
                echo 'Po??czenie nie mog?o zosta? utworzone: ' . $e->getMessage();
            }
        }

        public function createTable() {

                        $q = $this->pdo -> query('SELECT * FROM article');
                          while($row = $q->fetch()) {
                              echo $row['id'].' ';
                          }
                          $q->closeCursor();
        }
    }

    ?>

回答by ajmedway

As per the PHP manual for PDO::query

根据PDO::query的 PHP 手册

PDO::query() returns a PDOStatement object, or FALSE on failure.

PDO::query() 返回一个 PDOStatement 对象,或者失败时返回 FALSE。

It looks like your query is failing (on line 33) and thus returning a BOOLEAN (false), likely because at that point in execution, PDO has not connected to a database that contains a table called article. In the connect() method I see that it tries to connect to a db called 'generatordatabase'; ensure this connection is being made prior to calling createTable(), otherwise ensure that it contains a table called 'article'.

看起来您的查询失败(在第 33 行)并因此返回 BOOLEAN (false),可能是因为在执行时,PDO 尚未连接到包含名为article的表的数据库。在 connect() 方法中,我看到它尝试连接到名为“generatordatabase”的数据库;确保在调用 createTable() 之前建立此连接,否则确保它包含一个名为“article”的表。

I would recommend adding some more code examples, for instance the code that calls this class/method before the error is triggered.

我建议添加更多代码示例,例如在触发错误之前调用此类/方法的代码。

回答by Aris

Some error handling will help you avoid issues like this:

一些错误处理将帮助您避免这样的问题:

$q = $this->pdo->query('SELECT * FROM article');

//error case
if(!$q)
{
  die("Execute query error, because: ". print_r($this->pdo->errorInfo(),true) );
}
//success case
else{
     //continue flow
}

回答by Hossein

I'm not sure wheatear this is exactly the error I struggled with, but my error was due to my $con variable, I used a single $con for 2 SQL statements, for example:

我不确定 wheatear 这正是我遇到的错误,但我的错误是由于我的 $con 变量造成的,我对 2 个 SQL 语句使用了一个 $con,例如:

 $con = new mysqli($host,$username,$password,$database);
$sql = "SELECT name FROM users WHERE email = '$email'";

$stm = $con->prepare($sql);
$stm->execute();

and

 $sql1 = "INSERT INTO posts 
         VALUES('$email','$body')";


$stm1 = $con->prepare($sql1);


if ($stm1->execute()) {

I should have done:

我应该这样做:

$con = new mysqli($host,$username,$password,$database);
$sql = "SELECT name FROM users WHERE email = '$email'";

$stm = $con->prepare($sql);
$stm->execute();

and

$con1 = new mysqli($host,$username,$password,$database);

     $sql1 = "INSERT INTO posts 
             VALUES('$email','$body')";

    $stm1 = $con1->prepare($sql1);

    $stm1->execute()