php - Call to a member function fetch() on boolean -


i receive error:

fatal error: call member function fetch() on boolean in c:\xampp\htdocs\repo\generator\model\database.php on line 34

when run 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 * article');                           while($row = $q->fetch()) {                               echo $row['id'].' ';                           }                           $q->closecursor();         }     }      ?> 

as per php manual pdo::query

pdo::query() returns pdostatement object, or false on failure.

it looks query failing (on line 33) , returning boolean (false), because @ point in execution, pdo has not connected database contains table called article. in connect() method see tries connect db called 'generatordatabase'; ensure connection being made prior calling createtable(), otherwise ensure contains table called 'article'.

i recommend adding more code examples, instance code calls class/method before error triggered.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -