LCT1000 Internet 1 Internet 1 PHP and Databases. LCT1000 Internet 1 Topics Using SQL Common...
-
date post
22-Dec-2015 -
Category
Documents
-
view
220 -
download
1
Transcript of LCT1000 Internet 1 Internet 1 PHP and Databases. LCT1000 Internet 1 Topics Using SQL Common...
![Page 1: LCT1000 Internet 1 Internet 1 PHP and Databases. LCT1000 Internet 1 Topics Using SQL Common Queries PHP and MySQL.](https://reader030.fdocuments.us/reader030/viewer/2022032523/56649d775503460f94a5854e/html5/thumbnails/1.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022032523/56649d775503460f94a5854e/html5/thumbnails/2.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022032523/56649d775503460f94a5854e/html5/thumbnails/3.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022032523/56649d775503460f94a5854e/html5/thumbnails/4.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022032523/56649d775503460f94a5854e/html5/thumbnails/5.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022032523/56649d775503460f94a5854e/html5/thumbnails/6.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022032523/56649d775503460f94a5854e/html5/thumbnails/7.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022032523/56649d775503460f94a5854e/html5/thumbnails/8.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022032523/56649d775503460f94a5854e/html5/thumbnails/9.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022032523/56649d775503460f94a5854e/html5/thumbnails/10.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022032523/56649d775503460f94a5854e/html5/thumbnails/11.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022032523/56649d775503460f94a5854e/html5/thumbnails/12.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022032523/56649d775503460f94a5854e/html5/thumbnails/13.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022032523/56649d775503460f94a5854e/html5/thumbnails/14.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022032523/56649d775503460f94a5854e/html5/thumbnails/15.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022032523/56649d775503460f94a5854e/html5/thumbnails/16.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022032523/56649d775503460f94a5854e/html5/thumbnails/17.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022032523/56649d775503460f94a5854e/html5/thumbnails/18.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022032523/56649d775503460f94a5854e/html5/thumbnails/19.jpg)
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.](https://reader030.fdocuments.us/reader030/viewer/2022032523/56649d775503460f94a5854e/html5/thumbnails/20.jpg)
LCT1000 Internet 1
And Finally…
Always remember to close the connection
mysql_close($conn); Unless you are going to perform
more queries