ShawnDaGeek
ShawnDaGeek

Reputation: 4150

How to print or echo rows when using COUNT(*) and SUM()

I generated the below query for php using PHP-MYADMIN, My question is how to print the 30 rows it generates? when i use "see quote" it just errors out.

i am trying to echo the rows with search term and count in < div >< /div > tags each in its own.

  1. Facebook 38 searches
  2. Another Feed 25 searches
  3. Timeline 18 searches

and so on to row 30.


$result=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++;
        }

Screen Shot of Query in PHPmyadmin. enter image description here

Upvotes: 2

Views: 2370

Answers (3)

ShawnDaGeek
ShawnDaGeek

Reputation: 4150

I have found the solution to this. Thank you all for your guidance. :-) "you all rock"


$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 ')  ';
          }

I am able to print the array, hopefully i can get the loop down with a while statement. Feel free to add a loop if you have or know one that is better... Thank You.

Upvotes: 2

Haru
Haru

Reputation: 1381

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

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

mysql_close($con);

Upvotes: 2

Mosty Mostacho
Mosty Mostacho

Reputation: 43434

I think the best you can do is to take a look at this: mysql-fetch-array

There you'll find the answer on how to loop over that array :)

Upvotes: 4

Related Questions