Commit 38dc044f authored by Sorrel Harriet's avatar Sorrel Harriet
Browse files

adding demo code for lecture

parent d46075d0
<?php
// open a new MySQL database connection
// define a connection 'handle'
$link = mysqli_connect(
'localhost',
'sharr003',
'password123',
'sharr003_vendingmachine'
);
// check connection succeeded
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// create variable for page content HTML
$content = "<h1>Hot Drinks Menu</h1>";
// define the SQL query to run
$sql = "SELECT name, code, price FROM DrinkOption ORDER BY code";
// fetch records as a result-set
$result = mysqli_query($link, $sql);
// check query returned a result
if ($result === false) {
echo mysqli_error($link);
} else {
$content .= "<table>
<tbody>";
// fetch associative array
while ($row = mysqli_fetch_assoc($result)) {
$content .= "<tr>
<td>".$row['name']."</td>
<td>".$row['code']."</td>
<td> &pound;".$row['price']."</td>
</tr>";
}
$content .= "</tbody></table>";
// free result set
mysqli_free_result($result);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>My First PHP Application</title>
</head>
<body>
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="page2.php">Page 2</a></li>
</ul>
<?php
// Output value of $content variable
echo $content;
?>
</body>
</html>
DROP TABLE IF EXISTS Selection, DrinkOption;
CREATE TABLE DrinkOption (
code CHAR(2) UNIQUE NOT NULL,
drink_name VARCHAR(25) NOT NULL,
price DECIMAL(4,2) DEFAULT 0.80,
milk BOOLEAN NOT NULL,
PRIMARY KEY (code)
);
CREATE TABLE Selection (
id INT AUTO_INCREMENT,
option_code CHAR(2) NOT NULL,
sugars ENUM('0','1','2') NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (option_code)
REFERENCES DrinkOption (code)
);
INSERT INTO DrinkOption VALUES
('A','Black tea',DEFAULT,0),
('B','White tea',DEFAULT,1),
('C','Black coffee',DEFAULT,0),
('D','White coffee',DEFAULT,1);
INSERT INTO Selection VALUES
(NULL, 'A', '1'),
(NULL, 'D', '0');
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