0

my first post here and hoping someone can help. I am querying a table in a mySQL DB, and obviously getting the results. However, the table is used to store multiple entry by one user for the purpose of user contacts.

What I would like to do is display each user individually, and count the number of contacts each user has. I had a look at the post "How to detect duplicate posts in PHP array, which helped a bit, but I am still stuck.

Please see my code for the query below, I have left out the array duplicate part as it is a pretty mess at the moment.

<?php
    $result = mysql_query("SELECT * FROM vines");
    while($row = mysql_fetch_array($result)) {
        $results=$row['vinename'];
        echo $results;
        echo "<br />";
    }
?>

This result returns the below, obviously these are records from the vinename coloumn.

Marks Vine<br />
Marks Vine<br />
Marks Vine<br />
Tasch Vine<br />
Tasch Vine<br />

Regards Mark Loxton

4
  • Please fix your code formatting. There's a preview box under the editing box you can use to see if it looks reasonable. Commented Oct 28, 2009 at 18:24
  • 1
    whats your expected output? even if it is messy, can i see the array duplicate part? Commented Oct 28, 2009 at 18:25
  • So for this you want a list of the users and the count of how many times the user ("vinename") is listed? So for your example: "Marks Vine, 3; Tasch Vine, 2" ? Is that right? Commented Oct 28, 2009 at 18:26
  • Sorry about the vague coding Jani Commented Oct 28, 2009 at 19:27

7 Answers 7

1

Hi there, my first post here and hoping someone can help. I am querying a table in a mySQL DB, and obviously getting the results. However, the table is used to store multiple entry by one user for the purpose of user contacts.

You can do this in the query itself a lot more easily than in the PHP code afterwards.

SELECT name, COUNT(id) AS count FROM vines GROUP BY name
Sign up to request clarification or add additional context in comments.

Comments

1

Just change the SQL Query to

SELECT vinename, COUNT(vinename) as counter FROM vines GROUP BY vinename

and then do

echo $row['vinename']." #".$row['counter']."<br />";

1 Comment

Hi there, firstly thank you everyone for such awesome input. I seriously did not expect such a quick response. I am seriously greatful. The above worked like oil in an engine.
0

I would run two types queries... 1) Select each UNIQUE user from vines. 2) For each user in that set, run a second COUNT query against that user's id in the table "vines".

I hope that helps.

2 Comments

As other answers indicate, this can be done in a single query using the GROUP BY clause.
You should probably try to use the GROUP BY clause instead. it is a better solution.
0

You can create a separate array to store records you've already output there.

<?php
$result = mysql_query("SELECT * FROM vines");

$duplicates = array(); ## store duplcated names here
while($row = mysql_fetch_array($result)) {
    $results = $row['vinename'];

    if (!array_key_exists($results, $duplicates)) {
        echo $results;
        echo "<br />";
        $duplicates[$results] = 1; ## mark that we've already output this records
    }
}

?>

Comments

0

You can try, change your query to use count and group of SQL.

Somoe thing like

$result = mysql_query("SELECT count(*) as total,name FROM vines GROUP by name");

Comments

0

firstly thank you everyone for such awesome input. I seriously did not expect such a quick response. I am seriously grateful.

I used the recommendation from Jitter. I have pretty much been going through so many variations of the above code today, but just needed that missing piece.

Thanks, everyone. Below is what the final code looks like for anyone else who has the same problem in the future.

<?php

$result = mysql_query("SELECT vinename, COUNT(vinename) as counter FROM vines GROUP BY vinename ORDER BY counter DESC LIMIT 0, 3");
while($vinerow = mysql_fetch_array($result))

echo $vinerow['vinename']." has ".$vinerow['counter']." tomatos."."<br />";

?>

Comments

-1

change your query to:

SELECT distinct * FROM vines

2 Comments

Doesn't give counts, and it also wouldn't work if other data in the row isn't identical.
Thanks, as this has been my problem for the day, I have been going through everyones answer, and they all worked great.

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.