|
DATABASE
TABLE
MORE PHP
|
PHP and MySQL - Clean Up Database
DROP DATABASE csuxibqf_foo
CREATE DATABASE csuxibqf_foo
CREATE TABLE customer
(
customer_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(5),
last_name VARCHAR(20) NOT NULL,
first_name VARCHAR(20) NOT NULL,
date_of_birth DATE
)
INSERT INTO customer
(title, last_name, first_name, date_of_birth)
VALUES
("Mr", "Bloggs", "Fred", "1999-11-11")
INSERT INTO customer
(title, last_name, first_name, date_of_birth)
VALUES
("Ms", "Jane", "Mary", "1986-04-21")
INSERT INTO customer
(title, last_name, first_name, date_of_birth)
VALUES
("Mr", "Pompies", "Piet", "1966-01-31")
INSERT INTO customer
(title, last_name, first_name, date_of_birth)
VALUES
("Mr", "Doe", "John", "1982-08-02")
INSERT INTO customer
(title, last_name, first_name, date_of_birth)
VALUES
("Mr", "Jane", "Anne", "1982-07-24")
SELECT * FROM customer ORDER BY last_name, first_name
customer_id |
title |
last_name |
first_name |
date_of_birth |
1 |
Mr |
Bloggs |
Fred |
1999-11-11 |
4 |
Mr |
Doe |
John |
1982-08-02 |
5 |
Mr |
Jane |
Anne |
1982-07-24 |
2 |
Ms |
Jane |
Mary |
1986-04-21 |
3 |
Mr |
Pompies |
Piet |
1966-01-31 |
<!-- cleanUp.php -->
|
<?php
require "../secure.php"; // Contains host, username and password
// ===== DROP DATABASE ======================================================
// ===== Most certain way to clear garbage from tables and have a clean start
// ==========================================================================
$link = mysql_connect($host, // Host name
$user, // User name
$password) // User password
or die("<h2>Could not connect: </h2>" . mysql_error());
if (mysql_select_db ($database))
{
$sql1 = "DROP DATABASE $database";
$result = mysql_query($sql1, $link) or die(mysql_error());
}
// ===== RE-CREATE DATABASE =================================================
$sql2 = "CREATE DATABASE $database";
$result = mysql_query($sql2, $link) or die(mysql_error());
// ==========================================================================
// ===== SELECT DATABASE $database ==========================================
mysql_select_db ($database);
// ==========================================================================
// ===== RE-CREATE TABLE ====================================================
$sql3 = "CREATE TABLE customer
(
customer_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(5),
last_name VARCHAR(20) NOT NULL,
first_name VARCHAR(20) NOT NULL,
date_of_birth DATE
)";
mysql_query($sql3, $link) or die(mysql_error());
// ==========================================================================
// ===== POPULATE TABLE WITH SOME RECORDS ===================================
$sql4 = "INSERT INTO customer
(title, last_name, first_name, date_of_birth)
VALUES
(\"Mr\", \"Bloggs\", \"Fred\", \"1999-11-11\")";
mysql_query($sql4, $link) or die(mysql_error());
$sql5 = "INSERT INTO customer
(title, last_name, first_name, date_of_birth)
VALUES
(\"Ms\", \"Jane\", \"Mary\", \"1986-04-21\")";
mysql_query($sql5, $link) or die(mysql_error());
$sql6 = "INSERT INTO customer
(title, last_name, first_name, date_of_birth)
VALUES
(\"Mr\", \"Pompies\", \"Piet\", \"1966-01-31\")";
mysql_query($sql6, $link) or die(mysql_error());
$sql7 = "INSERT INTO customer
(title, last_name, first_name, date_of_birth)
VALUES
(\"Mr\", \"Doe\", \"John\", \"1982-08-02\")";
mysql_query($sql7, $link) or die(mysql_error());
$sql8 = "INSERT INTO customer
(title, last_name, first_name, date_of_birth)
VALUES
(\"Mr\", \"Jane\", \"Anne\", \"1982-07-24\")";
mysql_query($sql8, $link) or die(mysql_error());
// ==========================================================================
mysql_close($link);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META name="description" content="Tutorial for Linux, Apache, MySQL, SQL and PHP - LAMP">
<META name="keywords" content="tutorial, linux, apache, mysql, php, code, sql, lamp, lesson, lessons, example, examples, beginner, beginners, beginners' beginner's">
<title>PHP and MySQL - Clean Up Database</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<table border="1" cellspacing="0" cellpadding="4">
<tr valign="top">
<td><?php include("nav.php"); ?></td>
<td>
<h1>PHP and MySQL - Clean Up Database</h1>
<?php
echo "<p>".$sql1."</p>\n";
echo "<p>".$sql2."</p>\n";
echo "<p>".$sql3."</p>\n";
echo "<p>".$sql4."</p>\n";
echo "<p>".$sql5."</p>\n";
echo "<p>".$sql6."</p>\n";
echo "<p>".$sql7."</p>\n";
echo "<p>".$sql8."</p>\n";
require ("viewCode.php"); // Contains the viewData() code
viewData(); // Displays the SQL query results
dump_page(basename($_SERVER[SCRIPT_FILENAME])); // Display the code of this page
?>
</td>
</tr>
</table>
</body>
</html>
|
|