分解字符串并检查数据库中是否有重复项


Explode a String and Check Database for Duplicate

如何获取一个字符串business, jquery, cssexplode该字符串并检查mySQL数据库以查看每个单词是否已在数据库条目中?

谢谢鼓手392

$string = "business, jquery, css";
$exploded = explode(",", $string);
$result = /* database select query */
foreach ($exploded as $value) {
    // $value contains the value being processed
    // compare $result to $value and do what's needed
}

PHP 手册: explode()

希望这能让你知道你需要做什么。

编辑:感谢克里斯建议先进行数据库查询

$conn = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $conn);
$string  = "business, query, css";
$array = explode(", ", $string);
foreach($array as $str){
    $result = mysql_query("SELECT * FROM tableName WHERE columnName='$str'", $conn);
    $num_rows = mysql_num_rows($result);
    if($num_rows > 0)
        //found
    else
        //not found
} 

将 PHP 的 explode 函数与 ", " 分隔符一起使用,分配给一个新的数组变量。

我会改进之前的答案,我觉得最好在 foreach 循环之外使用 SELECT 查询。然后,您将避免执行相同的查询 3 次。

$result = /* database select query */
foreach ($exploded as $value) {
    // compare $result to $value and do what's needed
}