I'm trying to do a select, however I get the following messages on 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();
}
In which line 24 would be $stmt->execute();
I'm trying to return the values of fetchAll
with 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; ?>
DB Class:
<?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);
}
}
Crud class definition:
abstract class Crud extends DB {
Class definition Users :
class Usuarios extends Crud {
The definition of the table is by the class users:
protected $table = 'users
';
I'm relying on this generic crud .
If you need other parts of the code, I will make it available. Note: I am a lay person in PDO, I started my studies in about two days, so I apologize in advance for simple mistakes.