Click to See Complete Forum and Search --> : How do I count the number of unique rows, have some code


72newbie
07-01-2009, 01:12 AM
I'm looking for a fast lightweight way to count the results from a search.

this is what I have that seems to work:

$arr = array(11,22,33,11,22,33,44,55555,666666,666666);
foreach ($arr as $key => $word )
{
$words[] = $word;
}
$count = array_count_values($words);
foreach ($count as $key => $value)
{
echo "Number of occurrences:<b> $value </b>of word: <b>$key</b><br>\n";
}

but when I try to add a query into it it does not work...?


$id = "SELECT category_id FROM items ".$where_query." GROUP BY category_id DESC LIMIT 0,15";
$result = mysql_query($id) or die(mysql_error());
while($row = array($result))
{
foreach ($row['category_id'] as $key => $word )
{
$words[] = $word;
}
$count = array_count_values($words);
foreach ($count as $key => $value)
{
echo "Number of occurrences:<b> $value </b>of word: <b>$key</b><br>\n";
}

}



somehow i need to find the number of unique rows?

I am thinking maybe in this part is where i'm going wrong but i've been trying for days and I have to ask for help on this.

$result = mysql_query($id) or die(mysql_error());
while($row = array($result))

Dasher
07-01-2009, 04:39 PM
SELECT DISTINCT(category_ID) AS NUMCAT FROM mytable

ryanlund
07-03-2009, 05:07 AM
Here ya go mate


$id = "SELECT category_id FROM items ".$where_query." GROUP BY category_id DESC LIMIT 0,15";
$result = mysql_query($id) or die(mysql_error());

$num_results=mysql_num_rows($result);
echo "No. results found: ".$num_results."<br/>";
//get all the data below
while($row = mysql_fetch_array($result)){
echo "Category Id for row: ".$row['category_id']."<br/>";
}


Please be aware that it will not return more than 15 in one go, you would need to run a query without the LIMIT in it, and then run mysql_num_rows on that.

Ryan