<?php
$artist = '';
if(isset($_GET['artist']))
	$artist = $_GET['artist'];

// will attempt to connect to the database.
$dbconn = mysqli_connect("localhost", "root", "", "madisonsmusicdatabase");
if ($dbconn->connect_error > 0)
{
	die("cannot connect to db");
	exit();
}

// Will select ArtistID and ArtistName from the Artists table to display it on webpage.
$sql = $dbconn->prepare("SELECT ArtistID, ArtistName from Artists");
// Will execute query and return a result and display
$sql->execute();
$result = $sql->get_result();

// Create an array which will store the artist data.
$artistarray = array();
if($sql->affected_rows == 0)
{
	http_response_code(404);
	echo json_encode(
		array("message" => "No artists found")
	);
}
else
{
	while($row = $result->fetch_assoc())
	{
		$artistarray[] = $row;
	}
	http_response_code(200);
	header('Content-Type: application/json');
	header('Access-Control-Allow-Origin: *');
	header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
	header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description');
	$result = [
		'message' => 'Success',
		'artistlist' => $artistarray,
	];
	echo json_encode($result, JSON_PRETTY_PRINT);	
	
}
?>