【发布时间】:2017-05-16 19:27:09
【问题描述】:
我正在尝试构建搜索查询。我收到以下错误,似乎是 sql 的语法错误。
SQLSTATE[HY093]: 参数号无效(SQL: select * from products 其中样式 = 摘要、摘要和主题 = ? )
为什么会出现这个错误? 怎么弄出来的?
我的代码如下
if (isset($request->search)) {
//GET ALL INPUT FROM THE REQUEST
$query_strings = $request->all();
//PULL OUT ANY EMPTY FIELD FROM THE REQUEST
$filtered_array = array_filter($request->all());
//remove the last item
array_pop($filtered_array);
//BUILD A QUERY
$sql = array();
$values = array();
$x = 1;
foreach ( $filtered_array as $key =>$value ) {
if($x < count($filtered_array)){
$sql[]=" $key = ? and ";
$values[] =" $value , ";
} else {
$sql[]=" $key = ? ";
$values[] =" $value ";
}
$x++;
}
$fields = join(' ', $sql);
$v = join(' ',$values);
dd( \DB::select("select * from products where {$fields} ", [$v]));
}
【问题讨论】:
-
你说的是 Laravel,但你没有使用它 :( 你也能解释一下你在哪里出错以及你得到了什么错误。