使用COUNT(*)和SUM()时如何打印或回显行

时间:2011-10-03 00:13:52

标签: php mysql

我使用PHP-MYADMIN为php生成了以下查询, 我的问题是如何打印它生成的30行?当我使用“看到引用”它只是错误。

我正在尝试用搜索词回显行并计算< div>< / div>每个标签都有自己的标签。

  1. Facebook 38搜索
  2. 另一个Feed 25搜索
  3. 时间轴18搜索
  4. 等等到第30行。


      

    $结果= mysql_query($ SQL)


    $sql = 'SELECT COUNT(*) AS `Rows`, `search`, SUM(searched) FROM `af_timeline_search` GROUP BY `search` ORDER BY SUM(searched) DESC LIMIT 0, 30 ';
    

            /* top searches */
            $sqlthis = mysql_query('SELECT COUNT(*) AS `Rows`, `search` FROM `af_timeline_search` GROUP BY `search` ORDER BY `Rows` DESC');
            $num=mysql_num_rows($sqlthis);
            $arrS = mysql_fetch_array($sqlthis); 
            $i=0;
            while ($i < $num){
            echo 'Search '.$arrS[$i].'';
                $i++;
            }
    

    PHPmyadmin中的查询屏幕截图。 enter image description here

3 个答案:

答案 0 :(得分:4)

我认为你能做的最好的事情就是看看这个:mysql-fetch-array

你会找到关于如何循环该数组的答案:)

答案 1 :(得分:2)

$result = mysql_query("SELECT * FROM YOURDATABASE");

while($row = mysql_fetch_array($result))
  {
  echo $row['search'] . " " . $row['somenumber'];
  echo " searches";
  }

mysql_close($con);

答案 2 :(得分:2)

我找到了解决方法。谢谢大家的指导。 :-)“你们都摇滚”


$sql = "SELECT COUNT(*) AS Rows, search, SUM(searched) FROM anotherfeed.af_timeline_search GROUP BY search ORDER BY SUM(searched) DESC";
$result=mysql_query($sql) or die ('Error! yo.');
$row = mysql_fetch_array($result);
            echo '<pre>';
            print_r($row);
            echo '</pre>';

        /* top searches with loop */
        $sql = "SELECT COUNT(*) AS Rows, search, SUM(searched) FROM anotherfeed.af_timeline_search GROUP BY search ORDER BY SUM(searched) DESC LIMIT 0, 20";
        $result=mysql_query($sql) or die ('Error! yo.');
        while($row = mysql_fetch_array($result))
          {
          echo '  (';
         echo urldecode($row['search']) . " " . $row['SUM(searched)'] . "";
         echo ')  ';
          }

我能够打印数组,希望我能用while语句得到循环。如果你有或者知道一个更好的话,请随意添加循环...谢谢。