I am trying to perform a search for 2 fields, both are in the same table, I want to search for a data captured in the database either by the name of the equipment that is what is captured or by the date when it was captured.
For now I can only search for a single field, I can't make it search for any of the 2, or I search by name or by date, this is what I have.
This is the function by which I send the values to do the select.
public function Buscar($campo_buscar, $datos_buscar)
{
# code...
$conexion = $this->getConection();
$rows = array();
if ($datos_buscar == '') {
$query = $conexion->prepare("SELECT * from " . $GLOBALS['tabla']);
} else {
$query = $conexion->prepare("SELECT * from " . $GLOBALS['tabla'] . " WHERE " . $campo_buscar . " LIKE :b");
$query->bindValue(':b', '%' . $datos_buscar . '%', PDO::PARAM_STR);
}
if (!$query) {
return "Error al mostrar";
} else {
$query->execute();
while ($result = $query->fetch()) {
$rows[] = $result;
}
return $rows;
}
//$conexion->close();
}
And here I put the data that I want to search for, but in this case there are 2 not one,
// Buscar
$campo_buscar = NombreEquipobd;
$datos_buscar= $_POST['txtBuscar'];
//Buscar
$campo_buscarfecha = fechadeta;
$datos_buscartxtf = $_POST['txtfecha'];
Hello, you must place an AND to your statement:
you had to simplify more you can eliminate bindValue line why? It is simple because you are performing a search with a textSearch field that is of type String and then you want to perform a search with txtdate that is of type INT, therefore the restriction that you give from PDO::PARAM_STR would not give you good results, therefore write directly.
Use UNION to search for multiple values in the same table field
Ahem