Search code examples
expressionengine

expressionengine database class orderby and sort


How do we sort using the database class in expressionengine. orderby and sort are given an error and do not seem to work. I can't seem to find anything in the documantation about sorting results. This is what i have.

$results = $this->EE->db->query("
    SELECT plan_name
    FROM exp__plans 
    WHERE member_id='1002' AND orderby="id" sort="desc" LIMIT 1
");
$x = $results->row('plan_name')

;


Solution

  • There are issues with your query.

    try:

    $results = $this->EE->db->query("
        SELECT plan_name
        FROM exp_plans
        WHERE member_id = '1002' 
        ORDER BY id DESC LIMIT 1 
    ");
    

    I would recommend trying to run your query directly against the database if you're having trouble with it. 90% of the time it will be an issue with your SQL.

    Also, you are writing this in a add-on... right? if you're trying to get this to work within a template I would recommend checking out the query module.