Jump to content

[SOLVED] Counting unique rows


ShopMAster

Recommended Posts

I have a script that counts how many members have given good feedback for this person, but how do I specify how many unique members have given this person good feedback.  Meaining I don't want to count member1 2 times becuase he left feeback twice, I want to only count him once.

 

 

$total_num = mysql_result ( mysql_query("select count(*) as nr from feedback where rec ='".$_SESSION['id']."'") ,0,"nr" );

 

The above code is how I got the total number of feedbacks

 

Link to comment
https://forums.phpfreaks.com/topic/42704-solved-counting-unique-rows/
Share on other sites

I track members in the field 'giver' but when I put that in there it still is giving me two rows for one person giving 2 feedbacks.

 

Also when no one has given a feedback I get : Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 16 in D:\Domains\mwchallenge.com\wwwroot\myfeedback.php on line 27

 

Shouldn't it return 0 using the code above?

Here is the code I'm using.  The first one should give me the # of members who gave a positive feedback and the second for negative feedback.  Both should only give me a count for unique 'giver' fields, correct?

 

$nrfn = mysql_result ( mysql_query("select count(*) as nr from feedback where rec ='".$_SESSION['id']."' and rating = 3 GROUP BY giver") ,0,"nr" );

$nrfp = mysql_result ( mysql_query("select count(*) as nr from feedback where rec ='".$_SESSION['id']."' and rating = 1 GROUP BY giver") ,0,"nr" );

The reason being is the count(*).  Try this:

 

$nrfn = mysql_result ( mysql_query("select count(giver) as nr from feedback where rec ='".$_SESSION['id']."' and rating = 3 GROUP BY giver") ,0,"nr" );

$nrfp = mysql_result ( mysql_query("select count(giver) as nr from feedback where rec ='".$_SESSION['id']."' and rating = 1 GROUP BY giver") ,0,"nr" );

 

--FrosT

I get the following error when the count is 0;

 

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 16 in D:\Domains\mwchallenge.com\wwwroot\myfeedback.php on line 27

 

how do I just display 0 when I do <?=$nrfn?>

I don't use mysql_result. I usually use mysql_fetch_array or mysql_fetch_assoc. That is pretty simple. Either way to avoid the warning do this:

 

$nrfn = @mysql_result ( mysql_query("select count(giver) as nr from feedback where rec ='".$_SESSION['id']."' and rating = 3 GROUP BY giver") ,0,"nr" );

$nrfp = @mysql_result ( mysql_query("select count(giver) as nr from feedback where rec ='".$_SESSION['id']."' and rating = 1 GROUP BY giver") ,0,"nr" );

 

That should work.

 

--FrosT

Don't do this, period

$nrfn = mysql_result ( mysql_query("select count(giver) as nr from feedback where rec ='".$_SESSION['id']."' and rating = 3 GROUP BY giver") ,0,"nr" );

Do

$result = mysql_query("select count( distinct giver) as nr from feedback where rec ='".$_SESSION['id']."' and rating = 3 GROUP BY giver")  or die (mysql_error());

if (mysql_num_rows($result) > 0) {
   $nrfn = mysql_result($result, 0);
}
else $nrfn = 0;

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.