我正在尝试搜索 2 个字段,它们都在同一个表中,我想通过捕获的设备名称或捕获日期来搜索数据库中捕获的数据。
现在我只能搜索一个字段,我不能让它搜索 2 个中的任何一个,或者我按名称或日期搜索,这就是我所拥有的。
这是我发送值以进行选择的功能。
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();
}
在这里我放了我想要搜索的数据,但在这种情况下有 2 个不是一个,
// Buscar
$campo_buscar = NombreEquipobd;
$datos_buscar= $_POST['txtBuscar'];
//Buscar
$campo_buscarfecha = fechadeta;
$datos_buscartxtf = $_POST['txtfecha'];
你好,你必须在你的陈述中加上一个 AND:
你不得不简化更多你可以消除bindValue行为什么?这很简单,因为您正在使用 String 类型的 textSearch 字段执行搜索,然后您想使用 INT 类型的 txtdate 执行搜索,因此您从 PDO::PARAM_STR 给出的限制不会给您带来好处结果,所以直接写。
使用UNION在同一个表字段中搜索多个值
咳咳