Commit ad6e8894 authored by Sorrel Harriet's avatar Sorrel Harriet
Browse files

updating lab 5 example code

parent ea7c5d07
<?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;
?>
......@@ -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>&pound;".$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;
?>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment