benjy
benjy

Reputation: 4716

mysql_fetch_assoc returning results in the wrong order

I have the following code:

    $sortorder = $_GET['sort'];
switch($sortorder)
{
 case "modulea":
   $result2 = db_query("SELECT * FROM {vanqueue_registrations} WHERE isactive=1 ORDER BY `cleanmodule` ASC");
 case "moduled":
   $result2 = db_query("SELECT * FROM {vanqueue_registrations} WHERE isactive=1 ORDER BY `cleanmodule` DESC");
 case "typea":
   $result2 = db_query("SELECT * FROM {vanqueue_registrations} WHERE isactive=1 ORDER BY `cleantype` ASC");
   var_dump($result2);
 case "typed":
   $result2 = db_query("SELECT * FROM {vanqueue_registrations} WHERE isactive=1 ORDER BY `cleantype` DESC");
 default:
   $result2 = db_query("SELECT * FROM {vanqueue_registrations} WHERE isactive=1");
}
while($result = mysql_fetch_assoc($result2))
{
 var_dump($result);
 $value .= "<tr><td><center>" . $result['cleanmodule'] . "</center></td><td><center>" . $result['cleantype'] . "</center></td><td><center>" . $directions[$result['direction']] . "</center></td></tr>";
}

As you can see, it takes a GET variable and, depending on its value, executes a certain query. (Don't worry, this is only testing code, I'll obviously validate the input more later.)

My problem: the correct query is executing, but mysql_fetch_assoc seems to be reordering the rows in a way I don't want.

I 'm testing with the case typea, and when I execute the page, that call to var_dump is executed - I can see the result on the page. So the switch is definitely working.

But, the var_dump inside the while statement is outputting the rows ordered by the primary key in the table, not by, per my request, cleantype.

How can I make mysql_fetch_assoc output the rows the way I want them ordered?

TIA.

Upvotes: 0

Views: 486

Answers (1)

Franz
Franz

Reputation: 11553

You forgot to use break at the end of all your cases...

Therefore, your default query always got executed at last.

Upvotes: 6

Related Questions