使用变量PHP传递ORDER BY

时间:2013-09-29 14:12:39

标签: php mysql sql-order-by

我有下一个代码但是我没有通过使用ORDER BY获得所需的结果:

// OrderBy WHERE Conditions
if ($sort_by == "0")    $sortby_condition = "ORDER BY mv_user_ranking.ranking_date DESC ";
if ($sort_by == "1")    $sortby_condition = "ORDER BY mv_user_info.age DESC ";
if ($sort_by == "2")    $sortby_condition = "ORDER BY mv_user_info.sex DESC ";

$query ="SELECT * FROM mv_user_info 
         LEFT JOIN mv_user_lang_interested ON mv_user_lang_interested.uid = mv_user_info.uid        
         LEFT JOIN mv_user_disponibility   ON mv_user_disponibility.uid   = mv_user_info.uid    
         LEFT JOIN mv_user_ranking         ON mv_user_ranking.uid         = mv_user_info.uid   

         WHERE country ='$country' AND city = '$city' AND
               mv_user_lang_interested.english = '1' AND
               mv_user_lang_interested.english_level = '2' AND 
               mv_user_info.uid != '$uid'"

         .$sortby_condition.

         "LIMIT 0, 50";

echo $query;

$result = mysql_query($query) or die(mysql_error());

我做错了什么?

2 个答案:

答案 0 :(得分:1)

这看起来很有趣,好吧,试试吧

// OrderBy WHERE Conditions
//i don't know what was passed as $sort_by value
//perhaps you may try 
$sort_by = (integer)$sort_by;  //casting 

if ($sort_by == 0){$sortby_condition = "ORDER BY mv_user_ranking.ranking_date DESC ";}//if () if() three times not an optimize way, try if, elseif
elseif ($sort_by == 1){    $sortby_condition = "ORDER BY mv_user_info.age DESC ";}//write your code in block "{}" this may prevent your block of statement mix
elseif ($sort_by == "2"){    $sortby_condition = "ORDER BY mv_user_info.sex DESC ";}

$query ="SELECT * FROM mv_user_info 
         LEFT JOIN mv_user_lang_interested ON mv_user_lang_interested.uid = mv_user_info.uid        
         LEFT JOIN mv_user_disponibility   ON mv_user_disponibility.uid   = mv_user_info.uid    
         LEFT JOIN mv_user_ranking         ON mv_user_ranking.uid         = mv_user_info.uid   

         WHERE country ='$country' AND city = '$city' AND
               mv_user_lang_interested.english = '1' AND
               mv_user_lang_interested.english_level = '2' AND 
               mv_user_info.uid != '$uid'"

    .$sortby_condition.

    "LIMIT 0, 50";

echo $query;

$result = mysql_query($query) or die(mysql_error());

如果有任何问题随时问 你能更好地给出正确的文字,那么我们可以更好地回答你:)

答案 1 :(得分:0)

从技术上讲,我没有看到查询的任何问题,但您可以做一件事:在此查询上创建另一个查询,以便此查询成为子查询,然后从上面的查询中排序。您的代码将如下所示:

// OrderBy WHERE Conditions
if ($sort_by == "0")    $sortby_condition = "ORDER BY ranking_date DESC ";
if ($sort_by == "1")    $sortby_condition = "ORDER BY age DESC ";
if ($sort_by == "2")    $sortby_condition = "ORDER BY sex DESC ";

//Note I have removed the table name from order by clause because in the super-query we will only have column name.

$query ="
Select * From (
 SELECT * FROM mv_user_info 
     LEFT JOIN mv_user_lang_interested ON mv_user_lang_interested.uid = mv_user_info.uid        
     LEFT JOIN mv_user_disponibility   ON mv_user_disponibility.uid   = mv_user_info.uid    
     LEFT JOIN mv_user_ranking         ON mv_user_ranking.uid         = mv_user_info.uid   

     WHERE country ='$country' AND city = '$city' AND
           mv_user_lang_interested.english = '1' AND
           mv_user_lang_interested.english_level = '2' AND 
           mv_user_info.uid != '$uid'
     LIMIT 0, 50) as a ".$sortby_condition;

echo $query;

$result = mysql_query($query) or die(mysql_error());

我希望这会有所帮助