我的sql查询排序问题


Problems with Sorting on my sql query

function getGamesBySearch($criteria) {
try {
        $sortedBy = 'Name';
        $db = getDBConnection();
        $query = "SELECT * 
                    FROM game
                    WHERE Name LIKE :criteria
                    ORDER BY :sortBy DESC";
        $statement = $db->prepare($query);
        $statement->bindValue(':criteria', '%'.$criteria.'%');
        $statement->bindValue(':sortBy',$sortedBy);
        $statement->execute();
        $results = $statement->fetchAll();
        $statement->closeCursor();
        return $results;           // Assoc Array of Rows
    } catch (PDOException $e) {
        $errorMessage = $e->getMessage();
        include '../view/errorPage.php';
        die;
    }       
}

出于某种原因,我的关联数组总是回来在我的GameID顺序,当我需要它是在名称的顺序?

不能在SQL语句中使用占位符作为列名。

你只能使用它们作为值。

您的查询是按字面值字符串排序的-这对所有行都是相同的。

你在有效地做

ORDER BY 'Name'
不是

ORDER BY Name