我正在将 php 与 mysqli 一起用于 Web 项目。
假设我们需要找到标题为“She is like the wind”的歌曲。
如果我们确定用户会输入歌曲的歌词以便我们可以这样做(伪代码):
get input and apply explode()
concat each word with % between them
and execute query
使用此输入:“she the wind”,将转换为“%she%the%wind%”,即可找到歌曲。
但是当输入是这样的:“wind like”->“%wind%like%”时,就找不到歌曲了。
我想通过添加like %?% and
与单词数组长度一样多的次数来制作 $query 字符串。这样 $query 字符串就准备好了。
问题出现在绑定参数 $vars 参数上。
同样,$types 字符串可以通过以下方式连接:
$types .= "s"; //again, the number of times as the length of the array.
但是代表单词的变量呢?如果有 2 个单词,函数调用将类似于:
bind("ss", $word1, $word2)
要使用多少个 $word 变量?
我们可以设置要搜索的最大单词数,在示例 5 中,但这里我们遇到了另一个问题。不会使用的变量的内容是什么?例如,如果输入是 2 个单词,最后 3 个变量将包含什么?