-3
我有一個方法在一個PHP類,使它從數據庫中獲取數據。此方法有3個參數,並且考慮到這些參數中的1,2或3個參數不爲空,查詢數據庫的想法是。以下是上述方法:空參數到PHP類的方法
public function resultSearch($city = null, $price = null, $nr_dhomash = null){
$db = new Database;
$stmt = "";
if ($city != null && $price != null && $nr_dhomash !=null) {
$query = "Select * from postim where qyteti = :q and price <= :p and nr_dhoma=:n";
$stmt = $db->connect()->prepare($query);
$stmt->bindParam(":q", $qyteti);
$stmt->bindParam(":p", $price);
$stmt->bindParam(":n", $nr_dhomash);
}else if ($city != null && $price !=null) {
$query = "Select * from postim where qyteti=:q and price <= :p";
$stmt = $db->connect()->prepare($query);
$stmt->bindParam(":q", $qyteti);
$stmt->bindParam(":p", $price);
}else if ($city != null && $nr_dhomash !=null) {
$query = "Select * from postim where qyteti=:q and nr_dhoma=:n";
$stmt = $db->connect()->prepare($query);
$stmt->bindParam(":q", $qyteti);
$stmt->bindParam(":n", $nr_dhomash);
}else if ($price != null && $nr_dhomash !=null) {
$query = "Select * from postim where price <= :p and nr_dhoma=:n";
$stmt = $db->connect()->prepare($query);
$stmt->bindParam(":p", $price);
$stmt->bindParam(":n", $nr_dhomash);
}else if ($city != null) {
$query = "Select * from postim where qyteti=:q";
$stmt = $db->connect()->prepare($query);
$stmt->bindParam(":q", $city);
}else if($price != null){
$query = "Select * from postim where price <= :p";
$stmt = $db->connect()->prepare($query);
$stmt->bindParam(":p", $price);
}else if ($nr_dhomash != null) {
$query = "Select * from postim where nr_dhoma=:n";
$stmt = $db->connect()->prepare($query);
$stmt->bindParam(":n", $nr_dhomash);
}
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
我不知道爲什麼如果2或所有參數不爲空,此方法無法正常工作。有人可以幫忙嗎?
這是什麼問題?你錯了什麼? –
只有一個參數不爲空,如果有2個參數不爲空,或者所有3個參數都不爲空,它才返回null,否則它的工作方式就好像只有一個參數不爲null。 –