Jump to content

Most Common Mysql Field


Orionsbelter

Recommended Posts

In my mysql database i have a field that records peoples details one part of the details is their county/region they live in. It records the county in a normal varchar field however i need a simple php script that searching the database and finds the most common county so i can then return to the screen where the most popular region for my members.

 

Thank you for reading.

Link to comment
Share on other sites

mysql_query() returns a result resource, and you can't echo a result resource. You have to do something to get the values out of it, (typically) using one of the mysql_fetch_* functions. In this case, mysql_fetch_row() would be fine.

 

$query = "SELECT County, COUNT(County) FROM members GROUP BY County ORDER BY COUNT(County) DESC LIMIT 1";
$result = mysql_query($query);
$arr = mysql_fetch_row($result);
echo "Most popular county is: {$arr[0]}, with {$arr[1]} members.";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.