我试图找出一个字符串是否包含特定字符串表中的任何字符串,并将找到的字符串分配给变量,然后将其插入数据库。但是,即使明显存在匹配项,它似乎也无法找到匹配项。任何帮助,将不胜感激。
这是代码:
$stringToMatch = 'Good Morning';
$string_list = "SELECT string FROM strings";
$resultStringList = mysqli_query($link, $string_list) or die(mysqli_error($link));
$string_name = "Test";
while ($row = mysqli_fetch_array($resultStringList)) {
if (stristr($StringToMatch, $row)) {
$string_name = $row;
}
}
mysqli_query($link, "INSERT INTO table (string) VALUES ('$string_name')") or die(mysqli_error($link));
此时,表中将出现在字符串列中的“测试”。理想情况下,应在表字符串中插入“ Morning”一词
stristr()
返回从第一次出现针到结束为止的所有干草堆。
您正在从查询中获取数组。所以需要将字符串传递给您的stristr函数以进行匹配
while ($row = mysqli_fetch_array($resultStringList)) {
if (stristr($StringToMatch, $row['string'])) {// need to pass string instead of array
$string_name = $row['string'];// here pass string instead if array
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句