0

Descending sort type does not work with this syntax, please help.

<?php

require_once("../connection/conn.php");

$compare = $_GET["sortfield"];

if(isset($_GET["sortfield"])){
    if(isset($_GET["sorttype"]) == "asc"){  
        if($compare == "gender"){

            $holdData = $conn->prepare("SELECT * FROM informations ORDER BY gender ASC, first_name ASC");
            $holdData->execute();
            $getData = $holdData->fetchAll(PDO::FETCH_ASSOC);
            $newData = json_encode($getData);;
            print_r($newData);

        }elseif ($compare == "last_name") {

            $holdData = $conn->prepare("SELECT * FROM informations ORDER BY last_name ASC");
            $holdData->execute();
            $getData = $holdData->fetchAll(PDO::FETCH_ASSOC);
            $newData = json_encode($getData);;
            print_r($newData);

        }elseif ($compare == "first_name") {

            $holdData = $conn->prepare("SELECT * FROM informations ORDER BY first_name ASC");
            $holdData->execute();
            $getData = $holdData->fetchAll(PDO::FETCH_ASSOC);
            $newData = json_encode($getData);;
            print_r($newData);

        }elseif ($compare == "timestamp") {

            $holdData = $conn->prepare("SELECT * FROM informations ORDER BY timestamp ASC");
            $holdData->execute();
            $getData = $holdData->fetchAll(PDO::FETCH_ASSOC);
            $newData = json_encode($getData);;
            print_r($newData);
        }else{

        }

    }elseif(isset($_GET["sorttype"]) == "desc"){

        if($compare == "gender"){

            $holdData = $conn->prepare("SELECT * FROM informations ORDER BY gender DESC, first_name DESC");
            $holdData->execute();
            $getData = $holdData->fetchAll(PDO::FETCH_ASSOC);
            $newData = json_encode($getData);;
            print_r($newData);

        }elseif ($compare == "last_name") {

            $holdData = $conn->prepare("SELECT * FROM informations ORDER BY last_name DESC");
            $holdData->execute();
            $getData = $holdData->fetchAll(PDO::FETCH_ASSOC);
            $newData = json_encode($getData);;
            print_r($newData);

        }elseif ($compare == "first_name") {

            $holdData = $conn->prepare("SELECT * FROM informations ORDER BY first_name DESC");
            $holdData->execute();
            $getData = $holdData->fetchAll(PDO::FETCH_ASSOC);
            $newData = json_encode($getData);;
            print_r($newData);

        }elseif ($compare == "timestamp") {

            $holdData = $conn->prepare("SELECT * FROM informations ORDER BY timestamp DESC");
            $holdData->execute();
            $getData = $holdData->fetchAll(PDO::FETCH_ASSOC);
            $newData = json_encode($getData);;
            print_r($newData);
        }else{

        }

    }else{

    }
}else{
    echo json_encode(["Message"=>"No Sortfield and Sort type found!"]);
    echo "<br>";
    echo json_encode(["Message"=>"Please select the sortfield and sort type!"]);
}
  • 1
    descending sort type does not function with this syntax Unclear... what you mean? the query with DESC ordering option is sent to MySQL (how was this checked?) but the output sorting do not match? – Akina Feb 9 at 15:14
  • I would suggest that you build the query string first, then prepare and execute that in one place. Since this question is more related to php than database administration, you may get more answers in stackoverflow. – Lennart Feb 9 at 17:40
  • 1
    Please show us SQL code and not PHP! Many DBAs have been programmers, many have not - of those of us who have been, many have never used PHP (nor want to - i.e. me! :-) ). But seriously, try and do what you want through the mysql CLI (or MySQL Workbench) - if it works there, then the problem lies with your PHP code and should be posted on StackOverflow - if it doesn't work via the CLI then it's an SQL problem and is on-topic here. p.s. welcome to the forum! :-) – Vérace Feb 9 at 20:57
  • Please provide a brief set of input rows and show us both the "wrong" output and the desired output. – Rick James Feb 13 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.