ORDER BY with IF-ELSE statement (DESC doesn't work) Descending sort type does not work with this syntax, please help.

&lt;?php

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

$compare = $_GET["sortfield"];

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

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

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

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

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

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

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

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

        }

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

        if($compare == "gender"){

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

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

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

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

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

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

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

        }

    }else{

    }
}else{
    echo json_encode(["Message"=&gt;"No Sortfield and Sort type found!"]);
    echo "&lt;br&gt;";
    echo json_encode(["Message"=&gt;"Please select the sortfield and sort type!"]);
}
Welcome.

Please notice that you are in a wrong place. 

dba - here you can give your mysql code alone and ask for help. 

You might have asked this question in stackoverflow.

Anyway here is what you miss.

if(isset($_GET["sortfield"])){


is right. But

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


is not right as the expression to the left of equal condition operator would evaluate to true or false and if you check the logical value == "asc" it will fail. It should be

if(isset($_GET["sorttype"]) and $_GET["sorttype"]== "asc"){  


or simply

if($_GET["sorttype"]== "asc"){  


if you are sure that it is already set (as of the first if).

Also, please go through this before asking. https://stackoverflow.com/help/minimal-reproducible-example

If you had gone through the mentioned page, formatted your question clearly and asked at the right place, you would have got your answer on day one itself for sure.