我有这个使用LIKE和BETWEEN语句的mysql语句:
$stmt1 = $db->prepare("SELECT * FROM forsale WHERE `make` LIKE ?
OR `model` LIKE ? AND `year` BETWEEN ? AND ? OR `price` BETWEEN ?
AND ? LIMIT $start,$perpage");
$stmt1->execute(array($searchQuery1,$searchQuery2,$searchQuery3,
$searchQuery4,$searchQuery5,$searchQuery6));
$rows1 = $stmt1->fetchAll();
当我写下这样的陈述时:
$firstWorking = $db->prepare("SELECT * FROM forsale WHERE `make` LIKE ?
OR `model` LIKE ? LIMIT $start,$perpage");
它工作正常,当我隔离BETWEEN时也是如此:
$secondWorking = $db->prepare("SELECT * FROM forsale WHERE `year` BETWEEN ? AND ?
OR `price` BETWEEN ? AND ? LIMIT $start,$perpage");
所有LIKE语句都正常运行,但问题是当LIKE语句在它们前面时,BETWEEN语句不起作用。当我选择一个范围(例如1999-2008)时,不会返回任何结果,但是如果我选择具有指定模型或品牌的日期范围,它只显示模型和make的结果,忽略between语句。任何帮助表示赞赏!
答案 0 :(得分:1)
我在编程方面比一年前更聪明。当$ _GET变量为空时,我必须构建一个自定义解决方案,它们将被替换为“%%”通配符或自定义int,例如开始年份的'1982'。这是答案::
if(isset($_GET['selectMake'])){
$selectMake = '%'.$_GET['selectMake'].'%';
} else {
$selectMake = '%%';
}
if(isset($_GET['selectModel'])){
$selectModel = '%'.$_GET['selectModel'].'%';
} else {
$selectModel = '%%';
}
if(isset($_GET['engineType'])){
$engineType = '%'.$_GET['engineType'].'%';
} else {
$engineType = '%%';
}
if(isset($_GET['transmission'])){
$transmission = '%'.$_GET['transmission'].'%';
} else {
$transmission = '%%';
}
if(!empty($_GET['startYear'])){
$startYear = $_GET['startYear'];
} else {
$startYear = '1982';
}
if(!empty($_GET['endYear'])){
$endYear = $_GET['endYear'];
} else {
$endYear = '2099';
}
if(!empty($_GET['minPrice'])){
$minPrice = $_GET['minPrice'];
} else {
$minPrice = '5000.00';
}
if(!empty($_GET['maxPrice'])){
$maxPrice = $_GET['maxPrice'];
} else {
$maxPrice = '1000000.00';
}
$stmt1 = $db->prepare("SELECT * FROM forsale WHERE (`make` LIKE ? OR `model` LIKE ?) AND (`transmission` LIKE ?) AND (`year` BETWEEN ? AND ?) AND (`price` BETWEEN ? AND ?) AND (`engineType` BETWEEN ? AND ?) LIMIT ?,?");
$stmt1->execute(array($selectMake,$selectModel,$transmission,$startYear,$endYear,$minPrice,$maxPrice,$minEngine,$maxEngine,$start,$perpage));
$rows1 = $stmt1->fetchAll();