Query with like and or with PHP variable

Asked

Viewed 411 times

0

I have a form with two inputs one type text and one type date. I am trying to make a query where the result should be filtered either by name field or by birth date field or by two together. It turns out that the query works not only with the name field or only with the date field.If searching for the two fields works, but brings all the data based on what was typed in the two fields. I would like the query to filter through one of the two fields as well. Below is my function that performs the query.

public function findPacient($nome, $dataNasc)
    {
        $connection = new FirebirdConnection();

        $this->nomePaciente = $nome;
        $this->dataNascimento = $dataNasc;

        $sql = "SELECT REGISTRO_PRONTUARIO,NOME,DATA_NASCIMENTO,DOCUMENTO,TELEFONE FROM PRONTUARIO WHERE NOME LIKE '%".$this->nomePaciente."%' OR DATA_NASCIMENTO LIKE '%".$this->dataNascimento."%'";

        $data = $connection->conn->query($sql);

        $result = $data->fetchAll(PDO::FETCH_ASSOC);

        return $result;
    }

This is the file where I am instantiating my class. I am formatting the date to be the same as the format of the database but it did not help.

<?php

require '../../vendor/autoload.php';

use Classes\Pacient\Pacient;

$nomePaciente = strtoupper($_POST['paciente']);
$dataNascimento = date('d.m.Y', strtotime($_POST['dtNasc']));


$pacient = new Pacient();

echo '<pre>';
print_r($pacient->findPacient($nomePaciente, $dataNascimento));
  • Formatted to YYYY/MM/DD? And delimited the date with single quotes? Example: '2010/12/18' I didn’t get the like. Why not? DATA_NASCIMENTO = '" . $this->dataNascimento ."'"

  • 1

    Data field in mysql is yyyy-mm-dd Try to convert to this format

  • Hello friend I followed your tip and the user2274616 and it worked, the problem is that I was using like on the date and also was not using aguns Ifs to test empty values.

No answers

Browser other questions tagged

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