关于使用LIKE %
和CONCAT
进行搜索,我已经查了很多回复,但他们并不适合我。
基本上我试图在数据库中找到像%Croyden
和Surrey
这样的字词
下面的脚本工作正常,但是我想对包含Croyden
和Surrey
的单词使用%。我尝试了各种方法,但没有任何效果。
$locTown = "Croydon";
$locCounty = "Surrey";
$query = 'SELECT locTown, locCounty FROM location where locTown LIKE ? AND locCounty LIKE ? ';
if ($stmt = mysqli_prepare($link, $query)) {
mysqli_stmt_bind_param($stmt, "ss", $locTown, $locCounty);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $locTown, $locCounty);
/* fetch value */
mysqli_stmt_fetch($stmt);
while (mysqli_stmt_fetch($stmt)) {
}
echo "Location: $locTown - $locCounty\n";
/* close statement */
mysqli_stmt_close($stmt);
}
mysqli_close($link);
只是为了Clarity,在更新SQL和Fething结果后,现在可以正常工作。
$query = "SELECT locTown, locCounty FROM location where locTown LIKE CONCAT('%', ?, '%') AND locCounty LIKE CONCAT('%', ?, '%') ";
if ($stmt = mysqli_prepare($link, $query)) {
mysqli_stmt_bind_param($stmt, "ss", $locTown, $locCounty);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $locTown, $locCounty);
/* fetch value */
mysqli_stmt_fetch($stmt);
while (mysqli_stmt_fetch($stmt)) {
echo "Location: $locTown - $locCounty\n";
}
/* close statement */
mysqli_stmt_close($stmt);
}
mysqli_close($link);
答案 0 :(得分:-1)
由于查询中的%
连接不适用于OP,因此您可以尝试在绑定时连接%
:
mysqli_stmt_bind_param($stmt, "ss", '%'.$locTown, '%'.$locCounty);