SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

25
SYST 28043 Web Technologies Databases & MySQL

Transcript of SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

Page 1: SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

SYST 28043

Web Technologies

SYST 28043

Web Technologies

Databases & MySQL

Page 2: SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

04/20/23 Wendi Jollymore, ACES 2

Database OverviewDatabase Overview

Database:A collection of related TablesA database server could house many databases

Table:A collection of records that describe items in an entitySome sytems call this a “file”E.g. Employee Table contains Employee Records

Page 3: SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

04/20/23 Wendi Jollymore, ACES 3

Database OverviewDatabase Overview

Record:Describes a single itemCollection of Fields or ColumnsAlso called a Row

Field:A single data elementAlso called a columnDescribes a piece of an item

Page 4: SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

04/20/23 Wendi Jollymore, ACES 4

Database OverviewDatabase Overview

Page 5: SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

04/20/23 Wendi Jollymore, ACES 5

Database OverviewDatabase Overview

Page 6: SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

04/20/23 Wendi Jollymore, ACES 6

Database OverviewDatabase Overview

Primary KeyA special field that acts as a unique identifier for a recordMust be unique for each recordEach table must have oneExamples:

Student ID, Social Insurance #, Product ID

Page 7: SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

04/20/23 Wendi Jollymore, ACES 7

Database Overview - ExercisesDatabase Overview - Exercises

1. Besides Students, what other tables do you think would exist in a database for Sheridan College?

2. Define a structure for a table called Mp3s that you would use to keep track of all your MP3 files.

3. Make up three examples of records with field values for the Mp3s table in #2.

Page 8: SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

04/20/23 Wendi Jollymore, ACES 8

Using MySQLUsing MySQL

In order to create database-driven pages, you need a database serverWe will use MySQLYou will need to go in and create databases and tables that your pages can useWe’ll use the PHPMyAdmin tool!

Page 9: SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

04/20/23 Wendi Jollymore, ACES 9

Using MySQLUsing MySQL

Go to http://localhostLog in using the user name and password you set up when you installed XAMPPIn the left-hand menu, select phpMyAdminLog in using the root password you set up for your Sql server when you installed XAMPP

Page 10: SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

04/20/23 Wendi Jollymore, ACES 10

Using MySQLUsing MySQL

Click on the Databases tab and find “Create Database”

Name: MediaCollation: latin1_general_ci (optional)Click Create

Create the Table:Name: CdsNumber of fields: 4Click Go

Page 11: SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

04/20/23 Wendi Jollymore, ACES 11

Using MySQLUsing MySQL

Fill in the information for all four fields

See notes online for field details

Add a descriptionClick Save

Page 12: SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

04/20/23 Wendi Jollymore, ACES 12

Using MySQLUsing MySQL

We can’t use the root account for web pages

Too powerful for a regular site visitor

Add a Guest user:On main admin page, Users tabClick Add new userFill in the user name and passwordSelect Host: LocalFor Global Privileges, check SELECT, INSERT, UPDATE, DELETEClick Add User button bottom-right

Page 13: SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

04/20/23 Wendi Jollymore, ACES 13

Accessing the DBAccessing the DB

Create the form in the notesUse this to get user data to storePHP file will be used to retrieve the data and save it to the database table

Open a new PHP fileAdd variables for user name, host, password

Use the guest name and password you created

Page 14: SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

04/20/23 Wendi Jollymore, ACES 14

Accessing the DBAccessing the DB

To perform any task with your table data:

Connect to the database serverSelect the database you want to work withPerform the commands you want

Page 15: SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

04/20/23 Wendi Jollymore, ACES 15

Accessing the DBAccessing the DB

Connecting to the server:mysql_connect(host, user, passwd)

Connects to a database server with a specific user name and passwordReturns a reference to the database connection object

$dblink = mysql_connect($hostname, $user, $passwd)

or die ("Error: No connection to MySQL server\n");

Page 16: SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

04/20/23 Wendi Jollymore, ACES 16

Accessing the DBAccessing the DB

Selecting the database:mysql_select_db(db, conn)

Selects a specific database using a connection that has already been created

mysql_select_db($dbname, $dblink)

or die ("Error: MySQL database not selected\n");

Page 17: SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

04/20/23 Wendi Jollymore, ACES 17

Accessing the DBAccessing the DB

Executing an SQL statement:mysql_query(cmd, conn)

Executes a specific command or query using a database connectionIf cmd is a SELECT statement, function returns a set of recordsIf cmd is INSERT, DELETE, UPDATE, $result true if successful, false if not

$result = mysql_query($sql, $dblink)or die ("SQL query failed: $sql<br />".mysql_error());

Page 18: SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

04/20/23 Wendi Jollymore, ACES 18

Accessing the DBAccessing the DB

In your PHP file:At the top, add variables for host, user, passwordNormally these are stored somewhere elseIn the <body> tag:

Connect to the databaseSelect the Media table

Page 19: SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

04/20/23 Wendi Jollymore, ACES 19

Retrieving Form DataRetrieving Form Data

You sent your form data using method=“post”

To access in PHP file:$_POST[“fieldname”]

Fieldname is the value in the input element’s name=“” attribute

isset($_POST[“fieldname”]) functionReturns true if the field has a value

$cdTitle = (isset($_POST["title"])) ? $_POST["title"] : "";

Page 20: SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

04/20/23 Wendi Jollymore, ACES 20

Inserting RecordsInserting Records

The SQL INSERT statement allows you to add records:INSERT INTO tableName (f1, f2, f3, ...) VALUES (v1, v2, v3…);Inserts the values v1 into field f1, the value v2 into field f2, etc…You can build an SQL statement using the form data

Page 21: SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

04/20/23 Wendi Jollymore, ACES 21

Inserting RecordsInserting Records

Add the code to build the SQL INSERT queryExecute the queryIf the result returned is greater than 0, display a confirmation messageIf the result is 0 or less, display an error messageCheck the notes!

Page 22: SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

04/20/23 Wendi Jollymore, ACES 22

Inserting RecordsInserting Records

Use phpMyAdmin to check your table and see if the record was added!

Check the notes online for complete code solution

Page 23: SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

Displaying RecordsDisplaying Records

A SELECT query will select a specific set of records

SELECT fields FROM table WHERE conditionFields = comma delimited list of fields

If you want all fields, use * instead

WHERE clause is optionalSELECT id, lastName, firstName FROM StudentsSELECT * FROM Wine WHERE price > 50.0SELECT * FROM Wine WHERE estate LIKE ‘%Jackson%’

04/20/23 Wendi Jollymore, ACES 23

Page 24: SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

Displaying RecordsDisplaying Records

Once you run a query, you’ll want to get the results

Determine how many records are in the result set

$rows = mysql_num_rows($result);

Access a row in the result set$myRecord = mysql_fetch_array($result);

Access fields in a fetched rowecho “Name: “.$myRecord[“lastName”].”, “.$myRecord[“firstName”];

04/20/23 Wendi Jollymore, ACES 24

Page 25: SYST 28043 Web Technologies SYST 28043 Web Technologies Databases & MySQL.

Displaying RecordsDisplaying Records

Try the demos in the notes:Display all records in a tableDisplay only records between a user-specified min and max price

Do the search exercise

04/20/23 Wendi Jollymore, ACES 25