用布尔模式

时间:2018-01-27 14:11:02

标签: php mysql full-text-search

我有php代码从names of toys表中获取words并使用布尔模式全文搜索在toys_text表的toys字段中匹配这些单词并显示{{1} } {for} names of toys玩具和独特用户数在网页上。

玩具的名称可以是单个单词或单词组合。我们希望从users获取这些结果,其中toys字段包含来自toys_text

的单词组合的所有单词

(示例 - 如果words表中包含名为words的条目 - blue car bluecar应出现在toystoys_text中比赛场地)。随着时间的推移,数据库随着玩具的添加而被添加,玩具被删除已经过时。

我正面临着将数组正确转换为字符串以输入到布尔全文搜索的问题。我使用foreach循环并希望输出+(蓝色)+(汽车),但它只是将+(car)插入到布尔全文查询中。

echo $ query1 outputs

`select COUNT(*) as 'count', COUNT(DISTINCT tw.screen_name) AS 'cnt' from 
tweets tw join users us on tw.user_id=us.user_id WHERE MATCH (tweet_text) 
AGAINST (' +(car)' IN BOOLEAN MODE)`

虽然我希望查询输出为

`select COUNT(*) as 'count', COUNT(DISTINCT tw.screen_name) AS 'cnt' from 
tweets tw join users us on tw.user_id=us.user_id WHERE MATCH (tweet_text) 
AGAINST ('+(blue) +(car)' IN BOOLEAN MODE)`

这是我的代码 -

<?php
$query = "SELECT * FROM words ORDER BY `words`.`words` 
ASC";
$result = mysqli_query($con, $query);

$i = 1;            
while($row = mysqli_fetch_array($result)){                        
    $Word = $row['words'];
    $WordsArr = explode(" ", $row['words']);
    $query = "";

       if(count($WordsArr) > 1){
          foreach ($WordsArr as $value) {
                    $value1 = " +"."(".$value."*".")";
                    }
           $query1 = "select COUNT(*) as 'count', COUNT(DISTINCT 
           t.name) AS 'cnt' from toys t WHERE MATCH (toys_text) AGAINST 
          ('$value1' IN BOOLEAN MODE)";
        }else{
        $query1 = "select COUNT(*) as 'count', COUNT(DISTINCT t.name) AS 
       'cnt' from toys t WHERE 
        MATCH (toys_text) AGAINST ('$WordsArr[0]* IN BOOLEAN MODE')";
        }               
        $Data1 = mysqli_query($con, $query1);
        $total1 = mysqli_fetch_assoc($Data1);                     
            ?>                  
        <tr>
          <td><?php echo $i;?></td>
          <td><?php echo $row['words'];?></td>
          <td><a href="showtoys.php?word=<?php echo 
          urlencode($Word); ?>" target="_blank"><?php echo 
          $total1['count']; ?></a></td>
          <td><a href="showtoys.php?keyword=<?php echo urlencode($Word); 
          ?>" target="_blank"><?php echo $total1['cnt']; ?></a></td>                   
        </tr>
            <?php $i++;} ?> 

1 个答案:

答案 0 :(得分:1)

您的问题是您没有连接字符串:

foreach ($WordsArr as $value) {
    $value1 = " +"."(".$value."*".")";
}

应该是:

$value1 = '';
foreach ($WordsArr as $value) {
    $value1 .= " +"."(".$value."*".")";
}