diff --git a/lab-5/music-store-app/album.php b/lab-5/music-store-app/album.php new file mode 100755 index 0000000000000000000000000000000000000000..cba73d0c1768d6a0570468e20a4acb90ac8205a4 --- /dev/null +++ b/lab-5/music-store-app/album.php @@ -0,0 +1,76 @@ +<?php + +// open a new MySQL database connection + +// define a connection 'handle' +$link = mysqli_connect( + 'localhost', + 'sharr003', + 'password123', + 'sharr003_musicstore' +); + +// check connection succeeded +if (mysqli_connect_errno()) { + exit(mysqli_connect_error()); +} + +// define the SQL query to run (from lab 3 queries.sql!) +// use column aliases if necessary to make referencing fields in result-set easier +$sql = "SELECT t.name AS track, t.track_number, a.title AS album +FROM Track t +INNER JOIN Album a + ON t.album_upc=a.upc +WHERE a.upc='018274372722' +ORDER BY track_number ASC"; + +// query the database +$result = mysqli_query($link, $sql); + +// define a variable for HTML content string +$content = ""; + +// get number of rows in result-set +$row_cnt = mysqli_num_rows($result); + +// check if there are rows to display... +if ($row_cnt == 0) { + // if not, output a suitable message + $content .= "<p>The album you requested was not found!</p>"; +} else { + // otherwise, update the HTML in $content + + // get a single row from the result-set as an enumerated array + $row = mysqli_fetch_row($result); + // reference the first item in the array by index + $content .= "<h1>".$row[0]."</h1>"; + $content .= "<table cellpadding='4' border='1'> + <thead align='left'> + <tr> + <th>Track name</th> + <th>Track number</th> + </tr> + </thead> + <tbody>"; + // while there are rows, fetch each row as an associative array + while ($row = mysqli_fetch_assoc($result)) { + // append the content with more HTML containing row data + $content .= "<tr> + <td>".$row['track']."</td> + <td>".$row['track_number']."</td> + </tr>"; // reference a field value in the array by its key! + } + // free result set + mysqli_free_result($result); + + $content .= "</tbody></table>"; +} + +// close the connection to the database */ +mysqli_close($link); + +// output the HTML + +echo $content; + +?> diff --git a/lab-5/music-store-app/index.php b/lab-5/music-store-app/index.php index 19078c78cea4663a8dc137b912659dd87b1da096..ee4d29c0366e755fd5d8bacc1ee7a67ed4710296 100755 --- a/lab-5/music-store-app/index.php +++ b/lab-5/music-store-app/index.php @@ -5,9 +5,9 @@ // define a connection 'handle' $link = mysqli_connect( 'localhost', - 'YOUR_USERNAME', - 'YOUR_PASSWORD', - 'YOUR_DB_NAME' + 'sharr003', + 'password123', + 'sharr003_musicstore' ); // check connection succeeded @@ -15,30 +15,65 @@ if (mysqli_connect_errno()) { exit(mysqli_connect_error()); } -// define the SQL query to run (from lab 3!) -$sql = "SELECT Album.title, Album.price, Artist.first_name, Artist.last_name, Genre.name AS genre, (SELECT COUNT(*) FROM Track WHERE Album.upc=Track.album_upc) AS num_tracks FROM Album -INNER JOIN Artist - ON Album.artist_id=Artist.id +// define the SQL query to run (from lab 3 queries.sql!) +// use column aliases if necessary to make referencing fields in result-set easier +$sql = "SELECT Album.title, Album.price, Artist.first_name, Artist.last_name, Genre.name AS genre, (SELECT COUNT(*) FROM Track WHERE Album.upc=Track.album_upc) AS num_tracks +FROM Album +INNER JOIN Artist + ON Album.artist_id=Artist.id INNER JOIN Genre - ON Album.genre_id=Genre.id -ORDER BY title ASC"; + ON Album.genre_id=Genre.id +ORDER BY title ASC;"; + +// query the database $result = mysqli_query($link, $sql); -// check query returned a result -if ($result === false) { - echo mysqli_error($link); +// define a variable for HTML content string +$content = "<h1>Albums</h1>"; + +// get number of rows in result-set +$row_cnt = mysqli_num_rows($result); + +// check if there are rows to display... +if ($row_cnt == 0) { + // if not, output a suitable message + $content .= "<p>There are no albums in the databased!</p>"; } else { - // $row_cnt = mysqli_num_rows($result); - // echo $row_cnt - // fetch associative array */ + // otherwise, update the HTML in $content + + $content .= "<table cellpadding='4' border='1'> + <thead align='left'> + <tr> + <th>Album name</th> + <th>Artist name</th> + <th>Price</th> + <th>Genre</th> + <th>Number of tracks</th> + </tr> + </thead> + <tbody>"; + // while there are rows, fetch each row as an associative array while ($row = mysqli_fetch_assoc($result)) { - echo($row['title']." (".$row['genre'].") "); + // append the content with more HTML containing row data + $content .= "<tr> + <td>".$row['title']."</td> + <td>".$row['first_name']." ".$row['last_name']."</td> + <td>£".$row['price']."</td> + <td>".$row['genre']."</td> + <td>".$row['num_tracks']."</td> + </tr>"; // reference a field value in the array by its key! } // free result set mysqli_free_result($result); + + $content .= "</tbody></table>"; } // close the connection to the database */ -mysqli_close($link) +mysqli_close($link); + +// output the HTML + +echo $content; ?>