Question

[Solved] Call to a member function fetch() on boolean [duplicate]

I receive this error:

Fatal error: Call to a member function fetch() on boolean in
C:xampphtdocsrepogeneratormodeldatabase.php on line 34

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();
        }
    }

    ?>
Enquirer: marjanos

||

Solution #1:

As per the PHP manual for PDO::query

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

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’.

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

Respondent: ajmedway

Solution #2:

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
}
Respondent: Aris

Solution #3:

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:

 $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()
Respondent: Hossein

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy