MySQL Forums
Forum List  »  PHP

Help Needed Using COUNT(*) Function.....Please
Posted by: Graham Neville
Date: January 22, 2006 01:01PM

Hi All,

I am ripping my hair out over why this doesnt work.......and im sure its something really simple.

Basically I have a Database Of Data That I want To Print to a PHP Page, which will list in a Descending order of how many are in the same in a certain Field......

e.g

------------------------
FRUIT | COUNT(*)
------------------------
APPLES = 10
BANANAS = 9
PEACHES = 6
ORANGES = 2
-------------------------

Now I know what the command SQL command is :

"SELECT `FRUIT` , COUNT(*) FROM `mytable` GROUP BY `FRUIT` ORDER BY `COUNT( * )` DESC LIMIT 0, 30"

As I have tested this within phpMyAdmin and it works......

However I fail to print this to PHP.

I believe I need a Loop here in order to get all the rows and print them all

I have used

"
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)) {

}
"

SO my Code Looks Like

$query = "'SELECT `FRUIT` , COUNT(*) FROM `mytable` GROUP BY `FRUIT` ORDER BY `COUNT( * )` DESC LIMIT 0, 30 '";

$result = mysql_query($query);

while ($row = mysql_fetch_row($result)) {

?>

<?php echo ($row['FRUIT'])?>
<?php echo ($row['COUNT'])?>


<?php

}

mysql_free_result($result);
mysql_close();

?>

"

However This Fails,

I have tried using mysql_fetch_row and mysql_fetch_array and mysql_fetch_assoc but all show

"
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource"

Someone PLease Help Me



Edited 1 time(s). Last edit at 01/22/2006 01:34PM by Graham Neville.

Options: ReplyQuote


Subject
Written By
Posted
Help Needed Using COUNT(*) Function.....Please
January 22, 2006 01:01PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.