0

I am trying to retrieve a list of referring urls from a MySQL table (column referrer). The referrer column contain the same URL more than once, in which I want to count and print how many times the URL appears. Then print the counts in descending order.

I've got the following code, but obviously is wrong somewhere

$ref=$icdb->get_row("SELECT COUNT(referrer) AS frequency, referrer FROM url_log WHERE u = '".$dom."' GROUP BY referrer ORDER BY frequency DESC");


foreach ($ref as $details) {
   echo "<td>".$details['referrer']."</td><td>".$details['frequency']."</td>";
}

I have exported some of the sql data as an array:

$url_log = array(
  array('id' => '681','url_id' => '69','options' => '','created_day' => '20140128','created' => '1390945235','deleted' => '0','u' => 'rch.metwitter','referrer' => 'http://www.domain.com/','ipaddr' => '121.223.91.20','browser' => 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko'),
  array('id' => '685','url_id' => '69','options' => '','created_day' => '20140130','created' => '1391115029','deleted' => '0','u' => 'rch.metwitter','referrer' => 'http://www.domain.com/?page=custom&id=69','ipaddr' => '121.223.91.20','browser' => 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko'),
  array('id' => '686','url_id' => '69','options' => '','created_day' => '20140130','created' => '1391116005','deleted' => '0','u' => 'rch.metwitter','referrer' => 'http://www.domain.com/','ipaddr' => '121.223.91.20','browser' => 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko')
);

The foreach statement gives me:

3 3 h h

3
  • not obvious to me :-( Commented Jan 31, 2014 at 20:45
  • would be better to help us , provide some sample data . or fiddle Commented Jan 31, 2014 at 20:46
  • See the above edit @echo_Me Commented Jan 31, 2014 at 21:06

1 Answer 1

1

I think your problem is with COUNT(1). I suggest changing it to COUNT(referrer).

Sign up to request clarification or add additional context in comments.

2 Comments

I get the following: 3 3 h h
i cant find any other mistake inyour code.Your problem is somewhere else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.