如何获取一个字符串business, jquery, css
和explode
该字符串并检查 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()
希望这能让您了解您需要做什么。
编辑:感谢 Chris 建议先进行数据库查询
explode
使用带有“,”分隔符的PHP函数,分配给一个新的数组变量。
我会改进上一个答案,我觉得在 foreach 循环之外使用 SELECT 查询会更好。然后您将避免执行相同的查询 3 次。
$result = /* database select query */
foreach ($exploded as $value) {
// compare $result to $value and do what's needed
}
$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
}