<?php

   include "pdo_connection.php";

   $sql = "SELECT _9a_song.ID AS Song_ID, _9a_song.Artist_ID, _9a_song.Title AS Song_Title, _9a_arist.Name AS Artist_Name"
         ."  FROM _9a_song JOIN _9a_arist ON _9a_song.Artist_ID = _9a_arist.ID WHERE";

   $and = " ";

   if(isset($_REQUEST["Artist_ID"]) && $_REQUEST["Artist_ID"] != 0) {
       $filters[] = "Artist_ID = ?";
       $values = array($_REQUEST["Artist_ID"]);
   } else {
       $filters[] = "Artist_ID > ?";
       $values = array(0);
   }

   foreach ($filters as $filter) {
      $sql = $sql.$and.$filter;
      $and = " AND";
   }

   $sql = $sql." ORDER BY _9a_song.Title";

   $stmt = $pdo->prepare($sql);
   $stmt->execute($values);
   
   $rows = array();

   if($stmt->rowCount() == 0) echo json_encode(array("Error"=>"No songs available."));
   else {
      while($row = $stmt->fetch()) {
         $row_array["Song_ID"] = $row["Song_ID"];
         $row_array["Artist_ID"] = $row["Artist_ID"];
         $row_array["Song_Title"] = $row["Song_Title"];
         $row_array["Artist_Name"] = $row["Artist_Name"];
         array_push($rows, $row_array);
      }
      echo json_encode($rows);
   }

?>