Query does not return values

Asked

Viewed 52 times

0

None of my querys return values, since they have data in my database. What could be happening in my code that is resulting in this? Follows the Code:

<?php
require_once("Usuario.php");
require_once("Conexao.php");
require_once("Crud.php");
class DaoUsuario implements Crud{
    private $status;
    private $tabela;

    public function __construct(){
        $this->$status = conexao::getConexao();
        $this->$tabela = "usuario";

    }

    public function consultar($id) {
        $query = "SELECT * FROM {$this->tabela} WHERE id=:id";
        try{
            $operacao = $this->status->prepare($query);
            $operacao->bindValue("id:",$id, PDO::PARAM_INT);
            $operacao->execute();
            $getRow = $operacao->fetch(PDO::FETCH_OBJ);
            $usuario = $getRow->usuario;
            $senha = $getRow->senha;
            $objeto = new Usuario($id, $usuario, $senha);
            $objeto->setId($id);
            return $objeto;
        } catch (PDOException $erro) {
            echo $erro->getMessage();
        }
    }

    public function criar($objeto) {
        $id = $this->getNewIdUsuario();
        $usuario = $this->getUsuario();
        $senha = $this->getSenha();
        $query = "INSERT INTO {$this->tabela} (id,usuario,senha) VALUES(:usuario, :senha)";
        try{
            $operacao = $this->status->prepare($query);
            $operacao->bindValue(":id",$id, PDO::PARAM_INT);
            $operacao->bindValue(":usuario",$usuario, PDO::PARAM_STR);
            $operacao->bindValue(":senha", $senha, PDO::PARAM_STR);
            if($operacao->execute()){
                if($operacao->rowCount() > 0){
                    $objeto->setId($id);
                    return true;
                } else{
                    return false;
                }
            }else{
                return false;
            }
        } catch (PDOException $erro) {
            echo $erro->getMessage();
        }

    }

    public function deletar($parametro) {
        $query = "DELETE FROM {$this->tabela} WHERE id=:id";
        try{
            $operacao = $this->status->prepare($query);
            $operacao->bindValue(":id",$id, PDO::PARAM_INT);
            if($operacao->execute()){
                if($operacao->rowCount() > 0){
                return true;
            }else{
                return false;
            }
            }else{
                return false;
            }
        } catch (PDOException $erro) {
            echo $erro->getMessage();

        }

    }

    public function modificar($objeto) {
        $id = $objeto->getId();
        $usuario = $objeto->getUsuario();
        $senha = $objeto->getSenha();
        $query = "UPDATE {$this->tabela} SET usuario=:usuario, senha=:senha WHERE id=:id";
        try{
            $operacao = $this->status->prepare($query);
            $operacao->bindValue(":id",$id, PDO::PARAM_INT);
            $operacao->bindValue(":usuario",$usuario, PDO::PARAM_STR);
            $operacao->bindValue(":senha",$senha, PDO::PARAM_STR);
            if($operacao->execute()){
                if($operacao->rowCount() > 0){
                    return true;
                }else{
                    return false;
                }
            }else{
                return false;
            }

        } catch (PDOException $erro) {
            echo $erro->getMessage();

        }

    }
    private function getNewIdUsuario(){
        $query = "SELECT MAX(id) AS id FROM {$this->tabela}";
        try{
            $operacao = $this->status->prepare($query);
            if($operacao->execute()){
                if($operacao->rowCount() > 0){
                    $getRow = $operacao->fetch(PDO::FETCH_OBJ);
                    $idReturn = (int) $getRow->id + 1;
                    return $idReturn;
                }else{
                    throw new Exception("Ocorreu um erro no Banco de Dados");
                    exit();
                }
            }else{
                throw new Exception("Ocorreu um erro no Banco de Dados");
                exit();
            }
        } catch (PDOException $erro) {
            echo $erro->getMessage();

        }
    }

}

When I’m gonna do a session:

<?php
    session_start();
    require_once('../Classes/DaoUsuario.php');

    $autentificar = new DaoUsuario;
    $usuario = $_POST['usuario'];
    $senha = $_POST['senha'];

        $autentificar->consultar($id);
        if(PDO::FETCH_ASSOC($autentificar)>1){
        $_SESSION['id'] = $id;
        $_SESSION['usuario'] = $usuario;
        header('Location: painel-de-controle.php');
    }else{
        header('Location: administrador.php');
        session_destroy();
    }
?>

My connection:

<?php
class Conexao {
   private static $conexao;
   private function __construct(){}
   private function __clone() {}
   private function __wakeup() {}

   public static function getConexao(){
       if(!isset(self::$conexao)){
           try{
            $banco = 'mysql:host=meu_servidor;dbname=nome_banco';
            $usuario = 'meu_usuario';
            $senha = 'minha_senha';
       }catch (PDOException $erro){
       echo $erro->getMessage();
       //Encerrando
       exit();
            }
        } 
       return self::$conexao;
    }
}

And returns the following error: Fatal error: Uncaught Error: Call to a Member Function prepare() on null in Daousuario.php on line 18

  • By the way did not connect correctly, a look at user, password and bank name.

  • To make it easier to find errors when you have a file and line name, indicate in the codes the names of the files of each one, and comment on the line that is the line 18 that the error speaks! Your connection is returning null, checks the bank credentials and the host and anything shows the function code conexao::getConexao();

  • I’ll rephrase my question to show the connection...

  • It is normal, only if there is some syntax error where I am not seeing (the real names I hid for security reasons)

  • $this->$status should be $this->status

  • I changed and no change occurred (returning null yet)

  • Guys, thank you so much, I was missing a few things in my connection. As much as it gave another error (in the part of php sessions) already helped me enough. Thanks people!

Show 2 more comments

1 answer

1


I know little about PHP and the framework you use, but looking briefly at its object $this->status result of conexao::getConexao() is void. Check the callback from conexao::getConexao().

  • So it’s not returning anything, but the amazing that it’s normal, so much so that there’s no syntax error when I open my IDE

  • I got it. Thanks Tetri!

  • cool @Newbie, if my answer helped you in resolution, mark it as solution.

Browser other questions tagged

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