结果越来越多 - 可能是内存问题

时间:2014-05-06 07:19:33

标签: php

我有类似动作的迭代执行。在每次下一次迭代中,似乎结果都在加起来。

我使用了unset($word_count)但是没有改变。

以下代码有什么问题吗?

如果我单独检查每个结果,那么它会给出正确的结果,但对于下面的情况,它会为$word_count

提供更多结果
        $select_words = mysqli_query($con,"SELECT * from review_details where rate = 5 and category = 'italian' and isApi = 0");

        while ($row1 = @mysqli_fetch_array($select_words))

        {

            //echo $row1[review]."<br>";

//          echo $row1[rate]."<br>";

            $word.=$row1[adjective].",";        

        }

        //echo "Words are : $word";

        echo "<br/><br/><strong> 5 star word count </strong>";

        $word_count = array_count_values(str_word_count($word,1));

        arsort($word_count);

        echo "<br>";

        echo "<br>";

        //var_dump($word_count);

        print_r($word_count);

        unset($word_count);
        unset($select_words);
        $select_words = mysqli_query($con,"SELECT * from review_details where rate = 4 and category = 'italian' and isApi = 0");

        while ($row1 = @mysqli_fetch_array($select_words))

        {

            //echo $row1[review]."<br>";

//          echo $row1[rate]."<br>";

            $word.=$row1[adjective].",";        

        }

        //echo "Words are : $word";

        echo "<br/><br/><strong> 4 star word count </strong>";

        $word_count = array_count_values(str_word_count($word,1));

        arsort($word_count);

        echo "<br>";

        echo "<br>";

        //var_dump($word_count);

        print_r($word_count);

        unset($word_count);
        unset($select_words);
        $select_words = mysqli_query($con,"SELECT * from review_details where rate = 3 and category = 'italian' and isApi = 0");

        while ($row1 = @mysqli_fetch_array($select_words))

        {

            //echo $row1[review]."<br>";

//          echo $row1[rate]."<br>";

            $word.=$row1[adjective].",";        

        }

        //echo "Words are : $word";

        echo "<br/><br/><strong> 3 star word count </strong>";

        $word_count = array_count_values(str_word_count($word,1));

        arsort($word_count);

        echo "<br>";

        echo "<br>";

        //var_dump($word_count);

        print_r($word_count);


        unset($word_count);
        unset($select_words);
        $select_words = mysqli_query($con,"SELECT * from review_details where rate = 2 and category = 'italian' and isApi = 0");

        while ($row1 = @mysqli_fetch_array($select_words))

        {

            //echo $row1[review]."<br>";

//          echo $row1[rate]."<br>";

            $word.=$row1[adjective].",";        

        }

        //echo "Words are : $word";

        echo "<br/><br/><strong> 2 star word count </strong>";

        $word_count = array_count_values(str_word_count($word,1));

        arsort($word_count);

        echo "<br>";

        echo "<br>";

        //var_dump($word_count);

        print_r($word_count);

        unset($word_count);
        unset($select_words);
        $select_words = mysqli_query($con,"SELECT * from review_details where rate = 1 and category = 'italian' and isApi = 0");

        while ($row1 = @mysqli_fetch_array($select_words))

        {

            //echo $row1[review]."<br>";

//          echo $row1[rate]."<br>";

            $word.=$row1[adjective].",";        

        }

        //echo "Words are : $word";

        echo "<br/><br/><strong> 1 star word count </strong>";

        $word_count = array_count_values(str_word_count($word,1));

        arsort($word_count);

        echo "<br>";

        echo "<br>";

        //var_dump($word_count);

        print_r($word_count);

它为$ word_count

提供结果

1754 3815 4832 5174 5324

但实际上是

1754 2061 1017 342 150

1 个答案:

答案 0 :(得分:1)

您没有取消设置或重置$word。您可以使用$word附加到.=字符串。因此,计数始终是正确的,您正在计算连续追加单词的$word字符串。

如果您不希望在每次通话后不断累计单词数量,请使用=代替.=

例如,使用此

$word = $row1[adjective].",";  

而不是这个

$word.=$row1[adjective].",";  

或者,如果要继续追加$word字符串,请创建另一个变量以临时存储该数据库调用的字词,然后您可以执行计数。