gpt4 book ai didi

php - 使用php搜索mysql中字符串数组的所有元素

转载 作者:行者123 更新时间:2023-11-29 13:11:15 24 4
gpt4 key购买 nike

我需要搜索数据库字符串数组的所有元素,并返回有多少值与现有数据库条目匹配。

数据库名称=words,表名称=集合,word是要搜索的数组的列

<?php
$array=array('abc','xyz','lmn','pqr');
@ $db=new mysqli('localhost','root','','words');

if(mysqli_connect_errno()) {
echo 'Error:Could not connect to the database';
} else echo 'connected';

$db->select_db('words');

foreach($array as $s) {
$query="select * from collection where word = '%".$s."%'";
$result=$db->query($query);
}

if($result) echo $db->affected_rows;
$db->close();
?>

最佳答案

$result = 0;
foreach($array as $s) {
$query="select count(*) as number_match from collection where word = '%".$s."%'";
$row = mysql_fetch_row($db->query($query));
$result+= $row['number_match'];
}

编辑:更好的是:

$words = implode("%','%",$array);
$query="select count(*) as number_match from collection where word in ('%$words%')";
var_dump(mysql_fetch_row($db->query($query));

关于php - 使用php搜索mysql中字符串数组的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22063598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com