269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.
-
Upload
suzanna-rogers -
Category
Documents
-
view
212 -
download
0
Transcript of 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.
![Page 1: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/1.jpg)
269200 Web Programming
LanguageWeek 7
Dr. Ken Cosh
PHP and storage
![Page 2: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/2.jpg)
Storing Data
• Last week we built a calendar and displayed an appointment
• Sadly once you closed your browser, the information was lost
• This week we will look at storing data permanently
• Files
• Databases
![Page 3: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/3.jpg)
Files
• We can create and store data in a file using fopen();
• <?php$file = fopen("test.txt","r”);
//some code to be executed
fclose($file);?>
![Page 4: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/4.jpg)
Fopen Modes
![Page 5: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/5.jpg)
Writing to a File
• Using ‘fwrite();’
• <?php$file = fopen("test.txt","w");echo fwrite($file,"Hello World. Testing!");fclose($file);
?>
![Page 6: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/6.jpg)
Reading from a file
• Using feof() and fgets()
• <?php$file = fopen(“test.txt", "r") or exit("Unable to open file!");while(!feof($file)) { echo fgets($file). "<br>"; }fclose($file);?>
![Page 7: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/7.jpg)
Problems with Files?
• Large file sizes are difficult and slow to work with
• Searching for records within a flat file is not easy
• Concurrent access to files is a problem (although you can lock files)
• Inserting or deleting records from the middle of the file is difficult (random access vs. sequential access)
• Access levels to the data is limited (ie. who can delete, add and change the data)
![Page 8: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/8.jpg)
Databases (MySQL)
• MySQL is a database server
• MySQL is ideal for both small and large applications
• MySQL supports standard SQL
• MySQL compiles on a number of platforms
• MySQL is free to download and use
• The data in MySQL is stored in database objects called tables.
• A table is a collection of related data entries and it consists of columns and rows.
![Page 9: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/9.jpg)
Check out PHPMYADMIN
• http://localhost/phpmyadmin/
![Page 10: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/10.jpg)
MySQLi Extension
• There are three main API options when considering connecting to a MySQL database server:
• PHP's MySQL Extension
• PHP's mysqli Extension
• PHP Data Objects (PDO)
![Page 11: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/11.jpg)
Making a connection
• Procedural Style$link = mysqli_connect(“localhost",“username",“password",“databasename") or die("Error " . mysqli_error($link));
//Some Code
mysqli_close($link);
• OO Style$mysqli = new mysqli("localhost", “username", "password", “databasename");
// Some Code
$mysqli->close();
![Page 12: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/12.jpg)
Create a Database
$query = “CREATE DATABASE my_db”;
• Procedural Styleif (mysqli_query($link, $query) === TRUE) { printf("Table myCity successfully created.\n");}
• OO Styleif ($mysqli->query($query) === TRUE) { printf("Table myCity successfully created.\n");}
But why not just use phpmyadmin?
![Page 13: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/13.jpg)
Create a Table
• $sql = “CREATE TABLE Persons(FirstName varchar(15),LastName varchar(15),Age int)”;
But why not just use phpmyadmin?
![Page 14: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/14.jpg)
Primary Keys & Auto Increment
• $sql = “CREATE TABLE Persons (personID int NOT NULL AUTO_INCREMENT, PRIMARY KEY(personID),FirstName varchar(15),LastName varchar(15),Age int)”;
![Page 15: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/15.jpg)
Insert
• $query = “INSERT INTO Persons (FirstName, LastName, Age)VALUES ('Peter', 'Griffin',35)”;
$query = “INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Glenn', 'Quagmire',33)”;
![Page 16: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/16.jpg)
Select (Procedural)
if ($result = mysqli_query($link, "SELECT Name FROM City LIMIT 10")) { printf("Select returned %d rows.\n", mysqli_num_rows($result)); mysqli_free_result($result);}
![Page 17: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/17.jpg)
Select (OO)
• if ($result = $mysqli->query("SELECT Name FROM City LIMIT 10")) { printf("Select returned %d rows.\n", $result->num_rows); $result->close();}
![Page 18: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/18.jpg)
Select Result – Associative Array<?php//Open a new connection to the MySQL server$mysqli = new mysqli('host','username','password','database_name');
//Output any connection errorif ($mysqli->connect_error) { die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);}
//MySqli Select Query$results = $mysqli->query("SELECT id, product_code, product_desc, price FROM products");
print '<table border="1">';while($row = $results->fetch_assoc()) { print '<tr>'; print '<td>'.$row["id"].'</td>'; print '<td>'.$row["product_code"].'</td>'; print '<td>'.$row["product_name"].'</td>'; print '<td>'.$row["product_desc"].'</td>'; print '<td>'.$row["price"].'</td>'; print '</tr>';} print '</table>';
// Frees the memory associated with a result$results->free();
// close connection $mysqli->close();?>
![Page 19: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/19.jpg)
Select Result – Array<?php//Open a new connection to the MySQL server$mysqli = new mysqli('host','username','password','database_name');
//Output any connection errorif ($mysqli->connect_error) { die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);}
//MySqli Select Query$results = $mysqli->query("SELECT id, product_code, product_desc, price FROM products");
print '<table border="1"';while($row = $results->fetch_array()) { print '<tr>'; print '<td>'.$row["id"].'</td>'; print '<td>'.$row["product_code"].'</td>'; print '<td>'.$row["product_name"].'</td>'; print '<td>'.$row["product_desc"].'</td>'; print '<td>'.$row["price"].'</td>'; print '</tr>';
} print '</table>';
// Frees the memory associated with a result$results->free();// close connection $mysqli->close();?>
![Page 20: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/20.jpg)
Select Result – Object• <?php//Open a new connection to the MySQL server$mysqli = new mysqli('host','username','password','database_name');
//Output any connection errorif ($mysqli->connect_error) { die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);}
//MySqli Select Query$results = $mysqli->query("SELECT id, product_code, product_desc, price FROM products");
print '<table border="1">';while($row = $results->fetch_object()) { print '<tr>'; print '<td>'.$row->id.'</td>'; print '<td>'.$row->product_code.'</td>'; print '<td>'.$row->product_name.'</td>'; print '<td>'.$row->product_desc.'</td>'; print '<td>'.$row->price.'</td>'; print '</tr>';}
print '</table>';
// close connection $mysqli->close();?>
![Page 21: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/21.jpg)
WHERE
$qry = “SELECT * FROM Persons WHERE FirstName='Peter’”;
$qry = “SELECT * FROM Persons WHERE FirstName=‘$name’”;
![Page 22: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/22.jpg)
ORDER BY
$query = “SELECT * FROM Persons ORDER BY age”;
![Page 23: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/23.jpg)
UPDATE
$query = “UPDATE Persons SET Age=36 WHERE FirstName='Peter' AND LastName='Griffin‘”;
![Page 24: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/24.jpg)
DELETE
• $query = “DELETE FROM Persons WHERE LastName='Griffin‘”;
![Page 25: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/25.jpg)
Further Reading
• SQL Injections
• http://www.unixwiz.net/techtips/sql-injection.html
![Page 26: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/26.jpg)
Assignment• Extend the Appointment Calendar from last week.
• Allow users to store appointments and display them on the calendar.
FoE MtgTitle
13/12/2011Date
ASEAN-QA meeting, review the handbook first.
Detail
SubmitSubmit 2
Add AppointmentAdd Appointment View CalendarView Calendar
![Page 27: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/27.jpg)
Assignment
Foe Mtg in 1 day(s)
December 2011
3
2
Add AppointmentAdd Appointment View CalendarView Calendar
![Page 28: 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.](https://reader035.fdocuments.us/reader035/viewer/2022070401/56649f175503460f94c2d421/html5/thumbnails/28.jpg)
Assignment
FoE MtgTitle
13/12/2011Date
ASEAN-QA meeting, review the handbook first.
Detail
Appointment Detail
3
Add AppointmentAdd Appointment View CalendarView Calendar