我有一串由多个哈希(#)分隔的字符.我需要在php上的哈希之间得到单词.这是我的代码的样子:
$sql = "SELECT attribute_type.at_name,attribute_type.at_id FROM attribute_type
WHERE attribute_type.prodType_id = $pt_id
AND attribute_type.at_id NOT IN (SELECT at_id
FROM attribute_type
WHERE attribute_type.at_name = 'Product')";
while($items. strpos("#")>0){
// add the selected AT in every loop to be excluded
// .
// here tokens from $items are stored individually in
// $selectedAT (whose value changes in every loop/cycle)
//
// add to the current sql statement the values $at_id and $searchparam
$sql = $sql . "AND attribute_type.at_id NOT IN
(SELECT at_id FROM attribute_type
WHERE attribute_type.at_name = '$selectedAT')";
}
$dbcon = new DatabaseManager();
$rs = $dbcon->runQuery($sql);
Run Code Online (Sandbox Code Playgroud)
explode 通过在给定标记上拆分字符串来创建数组
$words = explode("#", $items);
Run Code Online (Sandbox Code Playgroud)
现在,如果您需要从字符串中提取这些单词并使用它们来比较SQL查询中的某些列...
$sql = "SELECT ... WHERE column IN ('" . implode("', '", $words) . "')";
Run Code Online (Sandbox Code Playgroud)
一旦在数组中包含单词,就不需要在循环中构建查询.
即使你确实想要这样做,你也不想为每个单词创建一个子查询,只要你可以在一个子查询中将单词组合在一起.