Fatal error & Pdoexception in SELECT POO PDO

Asked

Viewed 88 times

0

I am trying to perform a select, but I receive the following messages concerning the same line:

Fatal error: in C:\wamp\www\curso\Crud.class.php on line 24
PDOException: in C:\wamp\www\curso\Crud.class.php on line 24

Errors refer to the following method:

  public function findAll() {
        $sql = "SELECT * FROM $this->table";
        $stmt = DB::prepare($sql);
        $stmt->execute();
        return $stmt->fetchAll();
    }

On which line 24 would be $stmt->execute();

I’m trying to return the values of fetchAll with a foreach, as follows:

<?php foreach ($usuario->findAll() as $key => $value): ?>

<tbody>
<tr>
    <td><?php echo $value->id; ?></td>
    <td><?php echo $value->nome; ?></td>
    <td><?php echo $value->login; ?></td>
    <td><?php echo $value->senha; ?></td>                                    
</tr>
</tbody>
<?php endforeach; ?>

Class DB:

<?php

define('URL', 'http://localhost/t');
        const DB_NAME = 'curso';
        const DB_USER = 'root';
        const DB_PASS = '';
        const DB_HOST = 'localhost';

class DB {

    private static $instance;

    public static function getInstance() {
        if (!isset(self::$instance)) {
            try {
                self::$instance = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS);
                self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                self::$instance->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
            } catch (PDOException $e) {
                echo $e->getMessage();
            }
        }
        return self::$instance;
    }

    public static function prepare($sql) {
        return self::getInstance()->prepare($sql);
    }

}

Class definition Crud:

abstract class Crud extends DB {

Class definition Users:

class Usuarios extends Crud {

The table is defined by the user class: protected $table = 'users';

I’m basing myself on this generic crud.

If you need any other parts of the code, I’ll make it available. Note: I am layman in PDO, I started my studies on such two days, so I apologize in advance for simple mistakes.

  • 1

    the property $table is defined in its class and exists in DB ?

  • There is. I will fill in the question with more information.

  • I gave a Try/catch and displayed the following message: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'course.users' does not exist

  • @Thiagobarros You already have your answer, the table does not exist in the database!

  • that’s where ta.. the table exists yes!

  • Sorry. The error was in declaring another method in the DB class..

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.