MySQLi查询顺序为变量?

时间:2016-07-02 15:19:07

标签: variables mysqli

我有这个MySQLi查询:

$sqla = "SELECT * FROM `new_albums` WHERE `city_name` = '$city' AND `venue_name` LIKE '%$venue_name%' AND `amount_of_files` > '$img_count' AND (`album_date` >= '$date1' AND `album_date` <= '$date2') '$orderby' LIMIT $start_results, 12";

在删除之前它不起作用:

'$orderby'

$orderby变量由用户选择并且可以更改,因此它必须是变量:

if($order_by == 'most_viewed'){
        $orderby = '`viewed_count` DESC';
    }
    if($order_by == 'least_viewed'){
        $orderby = '`viewed_count` ASC';
    }
    if($order_by == 'date_created_desc'){
        $orderby = '`date_added` DESC';
    }
    if($order_by == 'date_created_asc'){
        $orderby = '`date_added` ASC';
    }
    if($order_by == 'most_files'){
        $orderby = '`amount_of_files` DESC';
    }
    if($order_by == 'least_files'){
        $orderby = '`amount_of_files` ASC';
    }

有什么方法可以让这个查询正常运行吗?

1 个答案:

答案 0 :(得分:1)

您需要包含MySQL关键字ORDER BY,例如

$orderby = 'ORDER BY `date_added` DESC';