<?php

require('../config/db.php');

function getOrders ($sort, $order) {
    global $servername, $username, $password, $dbname;

    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        if ($sort !== 'ID' && $sort !== 'Name' && $sort !== 'Created on' && $sort !== 'Status') {
            $order = 'ID';
        }
        if ($order !== 'ASC' && $order !== 'DESC') {
            $order = 'ASC';
        }

        return $conn->query("SELECT * FROM users ORDER BY `$sort` $order");
    } catch(PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
    }
}
