Null parameters to php class methods

时间:2017-08-05 10:31:22

标签: php mysql class pdo

I have a method in a php class that is made it to get data from database. This method has 3 parameters and the idea is to query database considering that 1, 2 or the 3 of these parameters are not null. Below is the method described above:

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;
    }

I don't know why this method doesn't work properly if 2 or all parameters are not null. Can someone help?

1 个答案:

答案 0 :(得分:1)

All this can be simplified to:

public function resultSearch($city = null, $price = null, $nr_dhomash = null){
    $db = new Database;
    $stmt = "";
    $where = [];
    $params = [];

    if ($city != null) {
        $where[] = 'qyteti = ?';
        $params[] = $city;
    }

    if ($price != null) {
        $where[] = 'price <= ?';
        $params[] = $price;
    }

    if ($nr_dhomash != null) {
        $where[] = 'nr_dhoma = ?';
        $params[] = $nr_dhomash;
    }

    $query = "Select * from postim";
    if ($where) {
        $query .= ' where ' . implode(' and ', $where);
    }
    $stmt = $db->connect()->prepare($query);
    $stmt->execute($params);

    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    return $result;
}