Many thanks for the tip about the $trings.
I get very confused what will work and what won't.
I have changed my script to make to slightly smaller.
I have tested again using count(*), and this is the result:
PHP Code:
$qry = "SELECT $field, COUNT(*) FROM $table WHERE $field IS NOT NULL GROUP by $field ORDER by COUNT(*) DESC LIMIT 6";
and
$qry = "SELECT $field, COUNT(*) FROM $table order BY COUNT(*)";
gives:
Quote:
Entertainer of the year award UK:
Henry Smith ()
Gary Perkins ()
John Permentor ()
Darren Busby ()
Richard Palmer ()
Total votes =
I now change it to:
PHP Code:
$qry = "SELECT $field, COUNT($field) FROM $table WHERE $field IS NOT NULL GROUP by $field ORDER by COUNT($field) DESC LIMIT 6";
and
$qry = "SELECT $field, COUNT($field) FROM $table order BY COUNT($field)";
gives:
Quote:
Entertainer of the year award UK:
Henry Smith (6)
Gary Perkins (3)
John Permentor (2)
Darren Busby (2)
Richard Palmer (1)
Total votes = 14
If by chance there is some other reason, here is the full script for entertainer:
PHP Code:
$tr=1;
//######################### ENTERTAINER #######################
$main_content .= '<td width=33% valign=top>';
$category = "Entertainer of the year award UK:";
$field = "entertainer";
$qry = "SELECT $field, COUNT($field) FROM $table WHERE $field IS NOT NULL GROUP by $field ORDER by COUNT($field) DESC LIMIT 6";
if(!($results = mysql_query($qry, $link))){
displayErrMsg(sprintf("Error in executing %s query", $qry));
displayErrMsg(sprintf("error:%d %s", mysql_errno($link), mysql_error($link)));
exit();
}
As I've been writing this reply, I remembered that I usd a similar code some time ago, using WHERE is NOT ???.
So I tried it in this script:
PHP Code:
$qry = "SELECT $field, COUNT($field) FROM $table WHERE $field != 'NULL' GROUP by $field ORDER by COUNT($field) DESC LIMIT 6";
I should have thought harder a few days ago and I might have figured this bit out.
However, I have never used COUNT before so I was really stuck without YOU !!
If you see any way of improving my script, I will be very happy to learn from you.
Very many THANKS....
__________________
The MAN, The MYTH, The LEGEND:
John C
________________________________
Support your local Country Music Club
The problem with COUNT(*) and COUNT($field) isn't the SQL it is the PHP.
That is because you aren't assigning a variable to COUNT(*) so you have no way of outputting that value as your PHP code is written.
Note too that this is incorrect: $field != 'NULL' the single quotes around NULL turn it into a string and not unknown or NULL. you also can't use != to test for NULL.
$qry = "SELECT $field, COUNT($field) FROM $table WHERE $field != 'NULL' GROUP by $field ORDER by COUNT($field) DESC LIMIT 6";
and
$qry = "SELECT $field, COUNT($field) FROM $table WHERE $field IS NOT NULL GROUP by $field ORDER by COUNT($field) DESC LIMIT 6";
and they BOTH give me the same result:
Quote:
Entertainer of the year award UK:
Henry Smith (6)
Gary Perkins (3)
John Permentor (2)
Darren Busby (2)
Richard Palmer (1)
Total votes = 14
Please understand, I'm NOT arguing with anybody, I'm just trying to learn and understand, being a relative novice.
Can you explain what the difference is between the two?
I have used !='???' prevoiusly, quite sucessfully:
PHP Code:
$qry = "SELECT * FROM ";
$qry .= $vars["table directory"];
$qry .= " WHERE category = 'ag'";
$qry .= "AND status != 'suspended' ";
$qry .= "AND status != 'unreviewed' ";
$qry .= " ORDER BY url DESC, name ASC" ;
I have used this code in my countrymusic.org.uk directories, in all categories.
Old Pendant:
I tested this code:
PHP Code:
$qry = "SELECT $field, COUNT(*) AS theCount FROM $table "
. " WHERE $field IS NOT NULL "
. " GROUP by $field "
. " ORDER by theCount DESC LIMIT 6";
and it returned:
Quote:
Entertainer of the year award UK:
Henry Smith ()
Gary Perkins ()
John Permentor ()
Darren Busby ()
Richard Palmer ()
Total votes = 14
I do want to thank you both for your input.
I have learned quite a lot while doing this project.
THANK YOU
__________________
The MAN, The MYTH, The LEGEND:
John C
________________________________
Support your local Country Music Club
You are not understanding, it isn't the sql but your php code that is poorly written.
At no point in your code will
COUNT(*) AS theCount
be printed out because nowhere in your php loop do you refer to theCount in order for it to be printed out.
In your WHILE loop you refer to this:
$list = $row[''.$field.''];
and THAT will never refer to theCount.
Code:
$qry = "SELECT $field as entertainer,
COUNT(*) as theCount
FROM $table
WHERE entertainer IS NOT NULL
GROUP BY
entertainer
ORDER by
thecCount DESC
LIMIT 6";
while($row = mysql_fetch_array($results)){
printf("%s (%d)", $row['entertainer'],$row['theCount']
}
in my code above I've skipped the part pushing the query in to $results. but you'll see how I refer to theCount in output part of the php WHILE.
In your query above you refer to $field which is fine EXCEPT you never refer to theCount in your output so therefore it will always show up blank.
So it isn't COUNT(*) and COUNT($field) that are just different (in that the latter skips counting NULL rows) it is that you never actually refer to the output of COUNT(*) when you used it that way.
You were lucky that PHP/MySQL allows that to work. Some languages and some databases don't carry an un-aliased field like that from DB to language. It's almost always a better idea to use an alias. (" ... AS theCount") and then use the alias in the PHP code.
If, for example, you did something like
Code:
SELECT SUM( IF(field IS NULL,0, field ) ...
it would be necessary. So good to get in the habit.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Thanks very much for trying to 'teach' me. I really do appreciate it.
If at any time you get fed up, please say so.
At leats I have the script working, albeit somewhat with 'poor' php, due to my inexperience.
I'm afrad that I have been experimenting all morning and getting nowhere.
$field = "entertainer"; // This is specified at the top of each category, which means that this is the only variable that I have to change in each category. There is currently 21 categories.
$list = $row[''.$field.''];
You were lucky that PHP/MySQL allows that to work. Some languages and some databases don't carry an un-aliased field like that from DB to language. It's almost always a better idea to use an alias. (" ... AS theCount") and then use the alias in the PHP code.
I still don't understand the principle of using ("%s (%d)", , and I can't find a satisfactory answer on Google.
The idea of usng $main_content .= is that at the end of the script I have this code:
Finally, I want to thank you, and guelphdad for the help and advice that you have given me. I certainly have a better general understanding of PHP/MySql now.
__________________
The MAN, The MYTH, The LEGEND:
John C
________________________________
Support your local Country Music Club
My script works fine.
In order to format it how I want it, this is the code that I have modified to:
PHP Code:
$field = "entertainer"; // This is specified at the top of each category, which means that this is the only variable that I have to change in each category. There is currently 21 categories.
$list = $row[''.$field.''];