How to know if a query has returned true

Asked

Viewed 56 times

1

I am registering users in the system, but I can not know the result of my query in the database I tried to return the value of execute but I was not successful, some solution?

Note: I am already able to enter the data in the database

My bank code

<?php 

    class Banco{

        private $conexao, $usuario;

        public function __construct(Conexao $conexao, Usuario $usuario){
            $this->conexao = $conexao->conectar();
            $this->usuario = $usuario;
        }
        public function inserir(){
            $query = 'insert into usuario(idUsuario, email, senha, nome) values(?, ?, ?, ?)';   
            $stmt = $this->conexao->prepare($query);
            $stmt->bindValue(1,$this->usuario->__get('idUsuario'));
            $stmt->bindValue(2,$this->usuario->__get('email'));
            $stmt->bindValue(3,$this->usuario->__get('senha'));
            $stmt->bindValue(4,$this->usuario->__get('nome'));
            $stmt->execute();
        }

        public function excluir(){

        }

        public function recuperarRegistros(){

        }

        public function pesquisarUsuario($id){
            $query = 'select idUsuario from usuario where id = ?';
            $stmt = $this->conexao->prepare($query);
            $stmt->bindValue(1,$this->usuario->__get('idUsuario'));
            $stmt->execute();
        }
    }


    ?>

Page calling the operation

<?php 
 require_once '../../projetoPrivado/Banco.php';
require_once '../../projetoPrivado/Conexao.php';
require_once '../../projetoPrivado/Usuario.php';

echo'<pre>';
    print_r($_POST);
echo '</pre>';
$conexao = new Conexao();
$usuario = new Usuario($_POST['nome'],$_POST['senha'], $_POST['email'], $_POST['id']);
$banco = new Banco($conexao, $usuario);
echo'<pre>';
    print_r($usuario);
echo '</pre>';
// $banco->inserir();

 $banco->pesquisarUsuario($_POST['id']);

?>

1 answer

2


Hello I made a modification in function pesquisarUsuario(), I don’t know if it’s gonna work out, but just run a test there and post the result for us...

public function pesquisarUsuario($id){
            $query = 'select idUsuario from usuario where id = ?';
            $stmt = $this->conexao->prepare($query);
            $stmt->bindValue(1,$this->usuario->__get('idUsuario'));
            $stmt->execute();
            $fetch = $stmt->fetch(PDO::FETCH_ASSOC);
            if(count($fetch)>0)
              return $fetch;
            else 
              return 'nenhum resultado encontrado';
        }
  • 1

    Although I find your answer valid, the question says.. "How to know if the query has returned true" ... What would be just return $stmt->execute();... But I think that’s what he really wants in his answer.

  • 1

    Thank you, that’s right

Browser other questions tagged

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