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!"]);
}
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 themysql
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 '20 at 20:57