Error in PDO listing

Asked

Viewed 218 times

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 ?

  • 1

    Or simply put a backslash ( ) on return $query->fetch( PDO::FETCH_ASSOC ); ... return $query->fetch( \PDO::FETCH_ASSOC );

  • Possible duplicate of Namespace and PDO = Error

  • but because other operations like insertion, deletion and update work. Only select does not work?

  • 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?

  • Because it’s the only one you’re using directly PDO::FETCH_ASSOC, the answer to the question I marked as duplicate.

  • 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!

Show 3 more comments

2 answers

3

You must indicate that you are using a native PHP class using before PDO leaving so:

new \PDO();

and

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

I think this should solve your problems. If you do not solve send me the error that PHP is returning with the changes I mentioned.

  • Hi, this had already solved. What I would like to know is why only with SELECT gave it? With UPDATE, DELETE and INSERT no need to use PDO

  • This is why with SELECT you request a return to the class "Return $query->fetch( PDO::FETCH_ASSOC );" already in others you have a default return.

  • Now you understand why SELECT is a problem?

  • I don’t know if I got it right. Look. Look at the registration method, last line: Return $execucao;. Isn’t this asking for a return to the method? I think I’m still a little confused. But I think I’ll be able to understand after this other answer of yours!

  • In the example you gave of the Return $execution the method returns only True or false, already in the select it returns either array, or in objects and etc.. in a way you need to inform the format that php will return to your query. which in case would be putting for example PDO::FETCH_ASSOC for it returns the query in the form of array.

  • hum, but without it it is already returning in array form. No? At least that’s what appears in print_r(). I solved the problem just by putting use PDO in the table class. But I’m still with this cruel doubt!

  • It returns why this default of it, you didn’t even need to have put the PDO::FETCH_ASSOC inside the function.

  • The chat is not working for me. But I understood!

  • I hope it helped, anything I’m up for.

  • I added an answer closer to what I needed because it only had your answer. Just to contribute a little more to the topic and help other people as well. But I appreciate your help!

Show 6 more comments

2

The solution to the doubt was to add

use PDO

to class Base as below:

<?php

 namespace BASE;

 use PDO;

 abstract class Base {
...

PDO is a class as well. Therefore, even though it is native, so it can bring different return from 0 ou 1, and True ou False, will need to be referenced!

Browser other questions tagged

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