Error in listing with PDO

1

I'm trying to learn PDO but I'm having difficulties.

I have 3 files:

Plans.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 run this script I get the error 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 not seem to find what a mistake this is!

The doubt is that it is not just reading.

But recording, deletion, editing, normal!

    
asked by anonymous 15.06.2018 / 23:31

2 answers

3

You should indicate that you are using a native PHP class using \ before the PDO leaving it as follows:

new \PDO();

and

 return $query->fetch( \PDO::FETCH_ASSOC );

I think this should solve your problems. If it does not resolve, please send me the error that PHP is returning with the changes I have made.

    
16.06.2018 / 02:01
2

The solution to the question was to add

use PDO

to class Base as below:

<?php

 namespace BASE;

 use PDO;

 abstract class Base {
...

PDO is a class too. So even if it is native, so that it can bring a different return from 0 ou 1 , and True ou False , it needs to be referenced!

    
16.06.2018 / 15:27