1
I’m trying to learn PDO
but I’m having a hard time.
I have 3 files:
Planos.php
<?php
namespace PLANOS;
use BASE\Base;
require_once "Base.php";
class Planos extends Base {
public function __construct ($_conexao, $_tabela) {
parent::__construct ($_conexao, $_tabela);
}
}
?>
Base.php
<?php
namespace BASE;
abstract class Base {
private $conexao;
private $tabela;
private $ultimoCadastrado;
public function __construct($_conexao, $_tabela) {
$this->conexao = $_conexao;
$this->tabela = $_tabela;
}
public function cadastrar (array $objeto) {
$parametros = $valores = "";
foreach ($objeto as $objeto=>$valor):
$parametros.= $objeto.",";
$valores.= "'".$valor."',";
endforeach;
$parametros = substr($parametros,0, -1);
$valores = substr($valores,0, -1);
$sql = "INSERT INTO ".$this->tabela." (".$parametros.") VALUES (".$valores.")";
$query = $this->conexao->prepare($sql);
$execucao = $query->execute();
$this->ultimoCadastrado = $this->conexao->lastInsertId();
return $execucao;
}
public function ultimoIdCadastrado () {
return $this->ultimoCadastrado;
}
public function editar (array $objeto, $id) {
$str = "";
foreach ($objeto as $objeto=>$valor):
$str .= $objeto ."='". $valor."',";
endforeach;
$str = substr($str, 0, -1);
$sql = "UPDATE ".$this->tabela." SET ".$str." WHERE id".ucfirst($this->tabela)."=".$id;
$query = $this->conexao->prepare($sql);
return $query->execute();
}
public function bloquear ($id, $bloqueado) {
$sql = "UPDATE ".$this->tabela." SET bloqueado = '".$bloqueado."' WHERE id".$this->tabela." = ".$id;
$query = $this->conexao->prepare($sql);
return $query->execute();
}
public function excluir ($id) {
$sql = "DELETE FROM ".$this->tabela." WHERE id".$this->tabela." = ".$id;
$query = $this->conexao->prepare($sql);
return $query->execute();
}
public function listar () {
$sql = "SELECT * FROM ".$this->tabela;
$query = $this->conexao->prepare($sql);
$query->execute();
return $query->fetchAll( PDO::FETCH_ASSOC );
}
public function listarId ($id = null) {
$sql = "SELECT * FROM ".$this->tabela;
if ($id != null) $sql .= " WHERE id".ucfirst($this->tabela)."=".$id;
$query = $this->conexao->prepare($sql);
$query->execute();
return $query->fetch( PDO::FETCH_ASSOC );
}
public function listarParametros ($where) {
$sql = "SELECT * FROM ".$this->tabela." ".$where;
$query = $this->conexao->prepare($sql);
$query->execute();
return $query->fetchAll( PDO::FETCH_ASSOC );
}
}
?>
Index.php
<?php
ini_set("display_errors",true);
ini_set("display_startup_erros",1);
error_reporting(E_ALL | E_NOTICE | E_STRICT);
use PLANOS\Planos;
$conexao = new PDO('mysql:host=localhost;dbname=funeraria2', 'root', 'mysql');
$objeto = array (
"tipo" => 's',
"nome" => "Carlos",
"login" => "caca",
"senha" => "1234",
"bloqueado" => "n"
);
$objeto2 = array (
"idAdministradores" => 3,
"tipo" => 's',
"nome" => "Castro",
"login" => "abcd",
"senha" => "4321",
"bloqueado" => "s"
);
require_once "Planos.php";
$planos = new Planos($conexao, "administradores");
// echo $planos->cadastrar($objeto) ? 1 : 2;
// echo "<br />";
echo $planos->ultimoIdCadastrado();
echo "<br />";
echo $planos->editar($objeto2, 3) ? 1 : 2;
echo "<br />";
echo $planos->bloquear(3, 's') ? 1 : 2;
echo "<br />";
echo $planos->excluir(4) ? 1 : 2;
echo "<br />";
print "<pre>";
print_r($planos->listar());
print_r($planos->listarId(1));
foreach ($planos->listarParametros("WHERE nome = 'Carlos'") as $admin):
print_r($admin);
endforeach;
print "</pre>";
?>
When I spin this script
get the error down below:
Fatal error: Uncaught Error: Class 'BASE\PDO' not found in D:\Trabalhos\host\htdocs\crud\Base.php:102
Stack trace:
#0 D:\Trabalhos\host\htdocs\crud\index.php(47): BASE\Base->listar()
#1 {main}
thrown in D:\Trabalhos\host\htdocs\crud\Base.php on line 102
But I can’t think what kind of mistake that is!
The question is you’re just not reading.
However recording, deletion, editing, normal!
Ever tried to import
use \PDO;
in the archive Tables.php ?– NoobSaibot
Or simply put a backslash ( ) on
return $query->fetch( PDO::FETCH_ASSOC );
...return $query->fetch( \PDO::FETCH_ASSOC );
– NoobSaibot
Possible duplicate of Namespace and PDO = Error
– NoobSaibot
but because other operations like insertion, deletion and update work. Only select does not work?
– Carlos Rocha
Then, use PDO; in the abstract class solved. But still the doubt. Why only for s SELECT? why not also for UPDATE, DELETE and INSERT?
– Carlos Rocha
Because it’s the only one you’re using directly
PDO::FETCH_ASSOC
, the answer to the question I marked as duplicate.– NoobSaibot
So, but my connection is already done by PDO. So, wouldn’t it be the case that you also need to use PDO in the Tables class? Otherwise, why? You can post as an answer I mark it as accepted!
– Carlos Rocha
Behold: Namespaces and Use when to use and for what purpose?, How namespaces work in PHP?, What good is namespace?
– NoobSaibot