PDO高级搜索查询构建问题


pdo advanced search query building issue

我正在尝试进行高级搜索,我有这个工作,但我正在为每个 get 变量进行全面查询,这不好,但它有效

现在我正在尝试根据 URL 变量是否为空来构建查询。但是我不知道为什么这不起作用。查询工作,因为我已经测试过了,我没有正确构建查询吗?

这是函数

public function search($man, $mod, $min, $max)
    {
        $this->currentLocation();
        $bind = '';
        $query .= 'SELECT listed_watches.*, watch.*, watch_images.* FROM listed_watches';
        $query .= 'LEFT JOIN watch ON watch.Id = listed_watches.watchID';
        $query .= 'LEFT JOIN watch_images ON listed_watches.url = watch_images.url';
        $query .= 'WHERE listed_watches.sellto REGEXP :search';
        if(!empty($man)){
            $query .= 'AND listed_watches.manufacture = :man';
            $bind  .= "$smt->bindValue(':man', $man);";
        }
        if(!empty($mod)){
            $query .= 'AND listed_watches.model = :mod';
            $bind  .= "$smt->bindValue(':mod', $mod);";
        }
        if(!empty($min)){
            $query .= 'AND listed_watches.minPrice > :min';
            $bind  .= "$smt->bindValue(':min', $min);";
        }
        if(!empty($max)){
            $query .= 'AND listed_watches.maxPrice < :max';
            $bind  .= "$smt->bindValue(':max', $max);";
        }
        $query .= 'GROUP BY listed_watches.id';
        $smt = $this->pdo->prepare(''.$query.'');
        $smt->bindValue(':search', "^[".$this->userLocation."]");
        $bind;
        return $smt->fetchAll();
    }

当我搜索并输入选择一个$man并$mon出现此错误

Undefined variable: smt 
Trying to get property of non-object in

此错误正在引用此行

$bind  .= "$smt->bindValue(':mod', $mod);";

首先你需要创建$smt对象,然后你可以对它使用方法。

    if(!empty($man)){
        $query .= 'AND listed_watches.manufacture = :man';
    }
    if(!empty($mod)){
        $query .= 'AND listed_watches.model = :mod';
    }
    // ...
    $query .= 'GROUP BY listed_watches.id';
    $smt = $this->pdo->prepare(''.$query.'');
    $smt->bindValue(':search', "^[".$this->userLocation."]");
    if(!empty($man)){
        $smt->bindValue(':man', $man);
    }
    if(!empty($mod)){
        $smt->bindValue(':mod', $mod);
    }