Top Banner
LCT1000 Internet 1 Internet 1 PHP and Databases
20

LCT1000 Internet 1 Internet 1 PHP and Databases. LCT1000 Internet 1 Topics Using SQL Common Queries PHP and MySQL.

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: LCT1000 Internet 1 Internet 1 PHP and Databases. LCT1000 Internet 1 Topics  Using SQL  Common Queries  PHP and MySQL.

LCT1000 Internet 1

Internet 1

PHP and Databases

Page 2: LCT1000 Internet 1 Internet 1 PHP and Databases. LCT1000 Internet 1 Topics  Using SQL  Common Queries  PHP and MySQL.

LCT1000 Internet 1

Topics

Using SQL Common Queries PHP and MySQL

Page 3: LCT1000 Internet 1 Internet 1 PHP and Databases. LCT1000 Internet 1 Topics  Using SQL  Common Queries  PHP and MySQL.

LCT1000 Internet 1

Structured Query Language

Text-based language widely used to interact with databases

Standard language Syntax similar to English Makes it easy to port your web applications

Page 4: LCT1000 Internet 1 Internet 1 PHP and Databases. LCT1000 Internet 1 Topics  Using SQL  Common Queries  PHP and MySQL.

LCT1000 Internet 1

Queries

All database transactions are handled by SQL Queries– Create or Drop tables– Alter table structure– Insert, Delete or Modify data– Select table contents to view

Often hidden by a GUI or web interface such as phpMyAdmin or Visual Studio

Page 5: LCT1000 Internet 1 Internet 1 PHP and Databases. LCT1000 Internet 1 Topics  Using SQL  Common Queries  PHP and MySQL.

LCT1000 Internet 1

Common Query Types

SELECT– Retrieve data from one or more tables

INSERT– Add new data to a table

UPDATE– Change one or more rows in a table

DELETE– Delete entries from a table

Page 6: LCT1000 Internet 1 Internet 1 PHP and Databases. LCT1000 Internet 1 Topics  Using SQL  Common Queries  PHP and MySQL.

LCT1000 Internet 1

Table ExamplesstudentID

Firstname

Lastname

0102030 Fred Bloggs

0304050 John Smith

0506070 Peter Flop

ModCode

Title Credits

LCT1000 Internet 1

20

LCT2506 Internet 2

20

LCT2512 Unix 20

Item

StudentID

ModCode

Mark Grade

1 0102030

LCT1000 65 B

2 0102030

LCT2506 55 C

3 0304050

LCT2506 48 D

Page 7: LCT1000 Internet 1 Internet 1 PHP and Databases. LCT1000 Internet 1 Topics  Using SQL  Common Queries  PHP and MySQL.

LCT1000 Internet 1

Viewing Data

SELECT * FROM results– Show all entries

SELECT ModCode, Mark FROM results– Select specific columns for all rows

SELECT ModCode, Mark FROM resultsWHERE StudentID='0102030'

Item StudentID ModCode Mark Grade

1 0102030 LCT1000 65 B

2 0102030 LCT2506 55 C

3 0304050 LCT2506 48 D

Page 8: LCT1000 Internet 1 Internet 1 PHP and Databases. LCT1000 Internet 1 Topics  Using SQL  Common Queries  PHP and MySQL.

LCT1000 Internet 1

Joining tables

SELECT students.First, students.Last, results.Mark

FROM students, resultsWHERE results.ModCode='LCT1000'AND results.StudentID = students.StudentIDORDER BY Mark

StudentID

First Last

0102030 Fred Bloggs

0304050 John Smith

0506070 Peter

Flop

Item

StudentID

ModCode Mark Grade

1 0102030 LCT1000 65 B

2 0102030 LCT2506 55 C

3 0304050 LCT2506 48 D

Page 9: LCT1000 Internet 1 Internet 1 PHP and Databases. LCT1000 Internet 1 Topics  Using SQL  Common Queries  PHP and MySQL.

LCT1000 Internet 1

Inserting Data

INSERT INTO results(StudentID, ModCode, Mark, Grade)VALUES ('0234567', 'LCT3007', 71, 'A')

Item

StudentID

ModCode Mark Grade

1 0102030 LCT1000 65 B

2 0102030 LCT2506 55 C

3 0304050 LCT2506 48 D

Page 10: LCT1000 Internet 1 Internet 1 PHP and Databases. LCT1000 Internet 1 Topics  Using SQL  Common Queries  PHP and MySQL.

LCT1000 Internet 1

Updating Data

UPDATE resultsSET Mark=65, Grade='B'WHERE StudentID='0304050'AND ModCode='LCT2506'

Item

StudentID

ModCode Mark Grade

1 0102030 LCT1000 65 B

2 0102030 LCT2506 55 C

3 0304050 LCT2506 48 D

Page 11: LCT1000 Internet 1 Internet 1 PHP and Databases. LCT1000 Internet 1 Topics  Using SQL  Common Queries  PHP and MySQL.

LCT1000 Internet 1

Deleting data

DELETE FROM results– Would delete all record - need WHERE

clause DELETE FROM results WHERE Item=3

Item

StudentID

ModCode Mark Grade

1 0102030 LCT1000 65 B

2 0102030 LCT2506 55 C

3 0304050 LCT2506 48 D

Page 12: LCT1000 Internet 1 Internet 1 PHP and Databases. LCT1000 Internet 1 Topics  Using SQL  Common Queries  PHP and MySQL.

LCT1000 Internet 1

SQL in your web pages

Page 13: LCT1000 Internet 1 Internet 1 PHP and Databases. LCT1000 Internet 1 Topics  Using SQL  Common Queries  PHP and MySQL.

LCT1000 Internet 1

5 Steps

Connect to the database Prepare an SQL query Send query to the database Process the results Close the connection

Page 14: LCT1000 Internet 1 Internet 1 PHP and Databases. LCT1000 Internet 1 Topics  Using SQL  Common Queries  PHP and MySQL.

LCT1000 Internet 1

Connecting to a database

<?php$user="ai1";$pass="fred";$database="ai1_db";$server="cet-mysql-01.cet.bolton.ac.uk";$conn=mysql_connect($server,$user,$pass);mysql_select_db ($database) or die ("oops");

?>

Page 15: LCT1000 Internet 1 Internet 1 PHP and Databases. LCT1000 Internet 1 Topics  Using SQL  Common Queries  PHP and MySQL.

LCT1000 Internet 1

Build a query

Assume we are going to build a query using form variables

Need to build a long string containing SQL and variables

$first = $_POST['first'];$last = $_POST['last'];$query = "INSERT into students (first,

last) VALUES ('$first','$last')";

Page 16: LCT1000 Internet 1 Internet 1 PHP and Databases. LCT1000 Internet 1 Topics  Using SQL  Common Queries  PHP and MySQL.

LCT1000 Internet 1

Send the query

For INSERT, UPDATE and DELETE:<?php

mysql_query($query);?> Possible to discover how many rows

affected using mysql_affected_rows()

Page 17: LCT1000 Internet 1 Internet 1 PHP and Databases. LCT1000 Internet 1 Topics  Using SQL  Common Queries  PHP and MySQL.

LCT1000 Internet 1

Send query (SELECT)

SELECT differs from the other queries because we expect it to return results

Typically step through the results a row at a time

Usually use a loop of some kind

Page 18: LCT1000 Internet 1 Internet 1 PHP and Databases. LCT1000 Internet 1 Topics  Using SQL  Common Queries  PHP and MySQL.

LCT1000 Internet 1

Result example

<?php$query = "Select first, last From student";$result = mysql_query($query);$num = mysql_numrows($result);

$i=0;while ($i < $num) { DO SOMETHING $i++;}

?>

Page 19: LCT1000 Internet 1 Internet 1 PHP and Databases. LCT1000 Internet 1 Topics  Using SQL  Common Queries  PHP and MySQL.

LCT1000 Internet 1

Inside the while loop

$first = mysql_result($result,$i,"first");$last = mysql_result($result,$i, "last"); Typically this gets printed on screen Often as a table

Page 20: LCT1000 Internet 1 Internet 1 PHP and Databases. LCT1000 Internet 1 Topics  Using SQL  Common Queries  PHP and MySQL.

LCT1000 Internet 1

And Finally…

Always remember to close the connection

mysql_close($conn); Unless you are going to perform

more queries