开发者

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.


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

Therefore, your default query always got executed at last.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜