Presentation is loading. Please wait.

Presentation is loading. Please wait.

269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage.

Similar presentations


Presentation on theme: "269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage."— Presentation transcript:

1 269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage

2 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

3 Files We can create and store data in a file using fopen();

4 Fopen Modes

5 Writing to a File Using ‘fwrite();’

6 Reading from a file Using feof() and fgets() "; } fclose($file); ?>

7 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)

8 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.

9 Check out PHPMYADMIN http://localhost/phpmyadmin/

10 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)

11 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();

12 Create a Database $query = “CREATE DATABASE my_db”; Procedural Style if (mysqli_query($link, $query) === TRUE) { printf("Table myCity successfully created.\n"); } OO Style if ($mysqli->query($query) === TRUE) { printf("Table myCity successfully created.\n"); } But why not just use phpmyadmin?

13 Create a Table $sql = “CREATE TABLE Persons ( FirstName varchar(15), LastName varchar(15), Age int )”; But why not just use phpmyadmin?

14 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 )”;

15 Insert $query = “INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Peter', 'Griffin',35)”; $query = “INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Glenn', 'Quagmire',33)”;

16 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); }

17 Select (OO) if ($result = $mysqli->query("SELECT Name FROM City LIMIT 10")) { printf("Select returned %d rows.\n", $result->num_rows); $result->close(); }

18 Select Result – Associative Array 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 ' '; while($row = $results->fetch_assoc()) { print ' '; print ' '.$row["id"].' '; print ' '.$row["product_code"].' '; print ' '.$row["product_name"].' '; print ' '.$row["product_desc"].' '; print ' '.$row["price"].' '; print ' '; } print ' '; // Frees the memory associated with a result $results->free(); // close connection $mysqli->close(); ?>

19 Select Result – Array 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 ' fetch_array()) { print ' '; print ' '.$row["id"].' '; print ' '.$row["product_code"].' '; print ' '.$row["product_name"].' '; print ' '.$row["product_desc"].' '; print ' '.$row["price"].' '; print ' '; } print ' '; // Frees the memory associated with a result $results->free(); // close connection $mysqli->close(); ?>

20 Select Result – Object 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 ' '; while($row = $results->fetch_object()) { print ' '; print ' '.$row->id.' '; print ' '.$row->product_code.' '; print ' '.$row->product_name.' '; print ' '.$row->product_desc.' '; print ' '.$row->price.' '; print ' '; } print ' '; // close connection $mysqli->close(); ?>

21 WHERE $qry = “SELECT * FROM Persons WHERE FirstName='Peter’”; $qry = “SELECT * FROM Persons WHERE FirstName=‘$name’”;

22 ORDER BY $query = “SELECT * FROM Persons ORDER BY age”;

23 UPDATE $query = “UPDATE Persons SET Age=36 WHERE FirstName='Peter' AND LastName='Griffin‘”;

24 DELETE $query = “DELETE FROM Persons WHERE LastName='Griffin‘”;

25 Further Reading SQL Injections http://www.unixwiz.net/techtips/sql-injection.html http://www.unixwiz.net/techtips/sql-injection.html

26 Assignment Extend the Appointment Calendar from last week. Allow users to store appointments and display them on the calendar. FoE Mtg Title 13/12/2011 Date ASEAN-QA meeting, review the handbook first. Detail Submit 2 Add Appointment View Calendar

27 Assignment Foe Mtg in 1 day(s) December 2011 3 2 Add Appointment View Calendar

28 Assignment FoE Mtg Title 13/12/2011 Date ASEAN-QA meeting, review the handbook first. Detail Appointment Detail 3 Add Appointment View Calendar


Download ppt "269200 Web Programming Language Week 7 Dr. Ken Cosh PHP and storage."

Similar presentations


Ads by Google