Sort array from a Query

This is a discussion on Sort array from a Query within the MySQL Database forums, part of the Database Forums category; I have 2 tables (simplified): coasters (COASTER_CODE, BREWERY_CODE, etc) breweries (BREWERY_CODE, BREWERY) For each brewery I want to count how ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-29-2005
bettina@coaster.ch
 
Posts: n/a
Default Sort array from a Query

I have 2 tables (simplified):

coasters (COASTER_CODE, BREWERY_CODE, etc)
breweries (BREWERY_CODE, BREWERY)

For each brewery I want to count how many coasters are there in table
"coasters" (up to here it works fine) and then I want to show them
ordered by quantity (that's my problem):

The following is the piece of code.

$search_breweries = mysql_query("SELECT b.BREWERY_CODE, b.BREWERY,
COUNT(c.COASTER_CODE) FROM breweries as b JOIN coasters as c ON
b.BREWERY_CODE = c.BREWERY_CODE Group by b.BREWERY_CODE order by
COUNT(c.COASTER_CODE) DESC ");
$i = 0;
while ($row = mysql_fetch_array($search_breweries, MYSQL_NUM)) {
$mybreweries[$i][0] = $row[0];
$mybreweries[$i][1] = $row[1];
$mybreweries[$i][2] = $row[2];
$i = $i + 1;
}
......
I get the following message:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL
result resource in....

Any help will be welcomed!

Reply With Quote
  #2 (permalink)  
Old 11-29-2005
Jerry Stuckle
 
Posts: n/a
Default Re: Sort array from a Query

bettina@coaster.ch wrote:
> I have 2 tables (simplified):
>
> coasters (COASTER_CODE, BREWERY_CODE, etc)
> breweries (BREWERY_CODE, BREWERY)
>
> For each brewery I want to count how many coasters are there in table
> "coasters" (up to here it works fine) and then I want to show them
> ordered by quantity (that's my problem):
>
> The following is the piece of code.
>
> $search_breweries = mysql_query("SELECT b.BREWERY_CODE, b.BREWERY,
> COUNT(c.COASTER_CODE) FROM breweries as b JOIN coasters as c ON
> b.BREWERY_CODE = c.BREWERY_CODE Group by b.BREWERY_CODE order by
> COUNT(c.COASTER_CODE) DESC ");
> $i = 0;
> while ($row = mysql_fetch_array($search_breweries, MYSQL_NUM)) {
> $mybreweries[$i][0] = $row[0];
> $mybreweries[$i][1] = $row[1];
> $mybreweries[$i][2] = $row[2];
> $i = $i + 1;
> }
> .....
> I get the following message:
> Warning: mysql_fetch_array(): supplied argument is not a valid MySQL
> result resource in....
>
> Any help will be welcomed!
>


Your query failed. Always check the return value from mysql_query:

$search_breweries = mysql_query("SELECT b.BREWERY_CODE, b.BREWERY,
COUNT(c.COASTER_CODE) FROM breweries as b JOIN coasters as c ON
b.BREWERY_CODE = c.BREWERY_CODE Group by b.BREWERY_CODE order by
COUNT(c.COASTER_CODE) DESC ");
if ($search_breweries) {
$i = 0;
while ($row = mysql_fetch_array($search_breweries, MYSQL_NUM)) {
$mybreweries[$i][0] = $row[0];
$mybreweries[$i][1] = $row[1];
$mybreweries[$i][2] = $row[2];
$i = $i + 1;
}
else
echo mysql_error();




--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote
  #3 (permalink)  
Old 11-29-2005
bettina@coaster.ch
 
Posts: n/a
Default Re: Sort array from a Query

Thank you for your answer. I get the message: Invalid use of group
function
Hat something to do with the "count" function?

Jerry Stuckle schrieb:

> bettina@coaster.ch wrote:
> > I have 2 tables (simplified):
> >
> > coasters (COASTER_CODE, BREWERY_CODE, etc)
> > breweries (BREWERY_CODE, BREWERY)
> >
> > For each brewery I want to count how many coasters are there in table
> > "coasters" (up to here it works fine) and then I want to show them
> > ordered by quantity (that's my problem):
> >
> > The following is the piece of code.
> >
> > $search_breweries = mysql_query("SELECT b.BREWERY_CODE, b.BREWERY,
> > COUNT(c.COASTER_CODE) FROM breweries as b JOIN coasters as c ON
> > b.BREWERY_CODE = c.BREWERY_CODE Group by b.BREWERY_CODE order by
> > COUNT(c.COASTER_CODE) DESC ");
> > $i = 0;
> > while ($row = mysql_fetch_array($search_breweries, MYSQL_NUM)) {
> > $mybreweries[$i][0] = $row[0];
> > $mybreweries[$i][1] = $row[1];
> > $mybreweries[$i][2] = $row[2];
> > $i = $i + 1;
> > }
> > .....
> > I get the following message:
> > Warning: mysql_fetch_array(): supplied argument is not a valid MySQL
> > result resource in....
> >
> > Any help will be welcomed!
> >

>
> Your query failed. Always check the return value from mysql_query:
>
> $search_breweries = mysql_query("SELECT b.BREWERY_CODE, b.BREWERY,
> COUNT(c.COASTER_CODE) FROM breweries as b JOIN coasters as c ON
> b.BREWERY_CODE = c.BREWERY_CODE Group by b.BREWERY_CODE order by
> COUNT(c.COASTER_CODE) DESC ");
> if ($search_breweries) {
> $i = 0;
> while ($row = mysql_fetch_array($search_breweries, MYSQL_NUM)) {
> $mybreweries[$i][0] = $row[0];
> $mybreweries[$i][1] = $row[1];
> $mybreweries[$i][2] = $row[2];
> $i = $i + 1;
> }
> else
> echo mysql_error();
>
>
>
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstucklex@attglobal.net
> ==================


Reply With Quote
  #4 (permalink)  
Old 11-29-2005
bettina@coaster.ch
 
Posts: n/a
Default Re: Sort array from a Query

Thank you. Now it works. I wrote like that:

$search_breweries = mysql_query("SELECT b.BREWERY_CODE, b.BREWERY,
COUNT(c.ID) as cant FROM breweries as b JOIN coasters as c ON
b.BREWERY_CODE = c.BREWERY_CODE Group by b.BREWERY_CODE order by cant
DESC ");

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 04:12 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0