爆炸字符串并检查数据库是否重复

时间:2012-05-08 23:20:20

标签: php mysql

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

谢谢, Drummer392

3 个答案:

答案 0 :(得分:3)

$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()

希望能让您知道自己需要做什么。

编辑:感谢Chris提出的首先进行数据库查询的建议

答案 1 :(得分:1)

使用PHP的explode函数和“,”分隔符,分配给新的数组变量。

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

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

答案 2 :(得分:1)

$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
}