View
222
Download
1
Category
Preview:
Citation preview
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
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
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
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
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
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
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
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
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
LCT1000 Internet 1
SQL in your web pages
LCT1000 Internet 1
5 Steps
Connect to the database Prepare an SQL query Send query to the database Process the results Close the connection
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");
?>
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')";
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()
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
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++;}
?>
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
LCT1000 Internet 1
And Finally…
Always remember to close the connection
mysql_close($conn); Unless you are going to perform
more queries
Recommended