gpt4 book ai didi

php - PDO高级搜索查询构建问题

转载 作者:行者123 更新时间:2023-11-29 23:12:00 25 4
gpt4 key购买 nike

我正在尝试进行高级搜索,我已经完成了这项工作,但我正在对每个获取变量进行完整查询,这不好,但它有效

现在我正在尝试根据 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);
}

关于php - PDO高级搜索查询构建问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28029432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com