Skip to content
Snippets Groups Projects
Commit 66915a6e authored by Benjamin Wells's avatar Benjamin Wells
Browse files

Adding all Lab Work - So Far

parents
Branches
No related merge requests found
Pipeline #20 skipped
Showing
with 245 additions and 0 deletions
<?php
// create variable for content HTML
$content = "<h1>Page not found</h1>";
$content .= "<p>Sorry, the page you requested could not be found.</p>";
// output the content HTML
echo $content;
?>
\ No newline at end of file
<?php
// create variable for content HTML
$content = "<h1>Hello and welcome to Goldsmith's Record Store</h1>";
$content .= "<p>Please follow the links above to browse the record store.</p>";
// output the content HTML
echo $content;
?>
\ No newline at end of file
File added
File added
File added
<?php
/* connect to the database */
$link = mysqli_connect(
'localhost',
'ma301bw',
'workdal1',
'ma301bw_anotherdb'
);
/* check connection succeeded */
if (mysqli_connect_errno()) {
exit(mysqli_connect_error());
}
$sql = "SELECT first_name, last_name FROM artist";
$result = mysqli_query($link, $sql);
if ($result === false) {
echo( "No records found." );
} else {
echo( "Some records were found!" );
}
/* check query returned a result */
if ($result === false) {
echo mysqli_error($link);
} else {
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
echo($row['first_name']." ".$row['last_name']);
}
/* free result set */
mysqli_free_result($result);
}
mysqli_close($link);
?>
\ No newline at end of file
<?php
require('includes/db_connect.php')
include('templates/header.html');
include('templates/navigation.html');
// get the value of the page parameter from the URL
// if no parameter detected...
if (!isset($_GET['page'])) {
$page_id = 'home'; // display home page
} else {
$page_id = $_GET['page']; // else requested page
}
switch ($page_id) {
case 'home' :
include 'views/home.php';
break;
case 'record' :
include 'views/record.php';
break;
case 'artist' :
include 'views/artist.php';
break;
default :
include 'views/404.php';
}
mysqli_close($link);
include('templates/footer.html');
include('templates/header.html');
?>
\ No newline at end of file
Live Demo
---------
This app can be viewed at:
http://doc.gold.ac.uk/~ma301bw/dnw/
\ No newline at end of file
<?php
// create variable for content HTML
$content = "<h1>Records</h1>";
// fetch records as a result set
$sql = "SELECT title, genre, price FROM record";
$result = mysqli_query($link, $sql);
// check query returned a result
if ($result === false) {
echo mysqli_error($link);
} else {
$content .= "<table border='1'><tbody>";
// fetch associative array
while ($row = mysqli_fetch_assoc($result)) {
$content .= "<tr>";
$content .= "<td>".$row['title']."</td>";
$content .= "<td>".$row['genre']."</td>";
$content .= "<td>".$row['price']."</td>";
$content .= "</tr>";
}
$content .= "</tbody></table>";
// free result set
mysqli_free_result($result);
}
// output the content HTML
echo $content;
?>
\ No newline at end of file
File added
File added
File added
DELETE FROM artist;
DELETE FROM record;
INSERT INTO artist (id, first_name, last_name)
VALUES
(NULL, 'Pink', 'Floyd'),
(NULL, 'David', 'Bowie'),
(NULL, 'Talking', 'Heads'),
(NULL, 'Roxy', 'Music'),
(NULL, 'Iggy', 'Pop')
(NULL, 'Led', 'Zeppelin');
INSERT INTO record (id, title, artist_id, genre, year, price)
VALUES
(NULL, 'Fear of Music', NULL, 'Rock', 1979, 20.99 ),
(NULL, 'Animals', NULL, 'Rock', 1977, 15.99 );
\ No newline at end of file
/* Simple query
Fetch first_name and last_name columns from artist table */
SELECT first_name, last_name FROM artist;
/* Query with filters
Fetches titles from record table where year is 1973 and genre is Reggae */
SELECT title FROM record
WHERE year = 1979
AND genre = "Rock";
\ No newline at end of file
/* Make sure tables don't exist before creation */
DROP TABLE IF EXISTS record, artist;
/* Define table for storing artists */
CREATE TABLE artist (
id INT AUTO_INCREMENT,
first_name VARCHAR(50),
last_name VARCHAR(50),
PRIMARY KEY(id)
);
/* Define table for storing records (products) */
CREATE TABLE record (
id INT AUTO_INCREMENT,
title VARCHAR(50),
artist_id INT,
genre TINYTEXT,
year YEAR(4),
price DECIMAL(10, 2) unsigned,
PRIMARY KEY (id),
FOREIGN KEY (artist_id)
REFERENCES artist (id)
);
DROP TABLE IF EXISTS 'table1', 'table2';
CREATE TABLE record (
id INT AUTO_INCREMENT,
title VARCHAR(50),
artist_id INT,
genre TINYTEXT,
year YEAR(4),
price DECIMAL(10, 2) unsigned,
PRIMARY KEY (id),
FOREIGN KEY (artist_id)
REFERENCES artist (id)
);
CREATE TABLE artist (
id INT AUTO_INCREMENT,
title VARCHAR(50),
artist_id INT,
genre TINYTEXT,
year YEAR(4),
price DECIMAL(10, 2) unsigned,
PRIMARY KEY (id),
FOREIGN KEY (album_id)
REFERENCES album (id)
);
CREATE TABLE genre (
id INT AUTO_INCREMENT,
title VARCHAR(50),
album_id INT,
genre TINYTEXT,
year YEAR(4),
price DECIMAL(10, 2) unsigned,
PRIMARY KEY (id),
FOREIGN KEY (album_id)
REFERENCES album (id)
);
CREATE TABLE label (
id INT AUTO_INCREMENT,
title VARCHAR(50),
album_id INT
genre TINYTEXT,
year YEAR(4),
price DECIMAL(10, 2) unsigned,
PRIMARY KEY (id),
FOREIGN KEY (artist_id)
REFERENCES artist (id)
);
File added
File added
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Record Store</title>
</head>
<body>
\ No newline at end of file
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