Displaying categories in a Drupal block

PHP code to display up to 30 categores.

<?php
if (user_access('access content')) 
{
	$result = db_query("SELECT d.tid, d.name, \ 
			COUNT(*) AS count FROM \
			{term_data} d INNER JOIN {term_node} \
			USING (tid) INNER JOIN {node} n USING (nid) \
			WHERE n.status = 1 GROUP BY d.tid, d.name \
			ORDER BY count DESC, d.name LIMIT 30");
	$items = array();
	while ($category = db_fetch_object($result))
	{
		$items[] = l($category->name .' ('. $category->count .')',
				'taxonomy/term/'. $category->tid);
	}
	return theme('item_list', $items);
}
?>

Source: Drupal support forum