当使用 2 个或更多单词时,此搜索方法可以正常工作。但如果只有一个词,它会给出错误:
SQLSTATE[HY093]:无效的参数号:绑定变量的数量与中的标记数量不匹配。
var_dump
给出$stmt
:
object(PDOStatement)#5 (1) {
["queryString"]=> string(117) "SELECT event_id, event_name FROM adrenaline_junkies_uk_events WHERE (`event_name` REGEXP ?) AND event_id <> ? LIMIT 3"
}
绑定的变量似乎加起来了。我在这里想念什么?
继承人的方法:
public function getSimilarEvents($event_id, $nameArray) {
$uniqueWords = array();
// list of common words we dont want to search by
$commonWords = array('a','able','you\'ve','z','zero');
// Build array of words that dont match common words
var_dump($uniqueWords = array_diff($nameArray, $commonWords));
// Count how many words there are
$wordCount = count($uniqueWords);
// create where string to add to query
$where_string = implode(" OR ", array_fill(0,$wordCount, "`event_name` REGEXP ?"));
// build start of mysql query
$query = "SELECT event_id, event_name FROM adrenaline_junkies_uk_events WHERE ($where_string) AND event_id <> ? LIMIT 3";
$stmt = $this->dbh->prepare($query);
$x = 0;
foreach($uniqueWords AS $index => $word){
$stmt->bindValue($index+1, "[[:<:]]".$word."[[:>:]]", PDO::PARAM_STR);
$x++;
}
$stmt->bindValue($x+1, $event_id, PDO::PARAM_INT);
$stmt->execute();
var_dump($stmt);
if($stmt->rowCount() > 0){
return $stmt->fetchAll();
}else{
return $stmt->rowCount();
}
}