Presentation is loading. Please wait.

Presentation is loading. Please wait.

COM621: Advanced Interactive Web Development Lecture 10 PHP and MySQL.

Similar presentations


Presentation on theme: "COM621: Advanced Interactive Web Development Lecture 10 PHP and MySQL."— Presentation transcript:

1 COM621: Advanced Interactive Web Development Lecture 10 PHP and MySQL

2 Web + PHP + MySQL Apache PHP MySQL WAMP/MAMP  Required Tools Text Editor or a Program like Dreamweaver

3 Installing WAMP/MAMP  Windows: Download and Install WAMP Server  MAC: Download and Install MAMP  Leave all the settings as default except the browser (change to your preferred browser: I recommend Chrome, Firefox or Safari)  Once Installed, your files for this module need to be stored in the following folder (also applies to the lab machines): c:/wamp/www/ Task: Create a folder with your name inside this folder

4 Configuring WAMP  Start Wampserver: a red “w” icon should appear in your taskbar, it should go from red, to orange to green – once it is green we are ready to go

5 WAMP in Browser

6 home pop-up sql window databases manage users import data export db db status phpMyAdmin help mysql help phpMyAdmin interface

7 Creating the database From the main menu choose Databases In the create database field type in a name for your database. Leave the collation drop down box if you wish to use the default MySQL schema collation. Click Create.

8 Setup a new user To setup a new user login to access this database, click on Users in the main menu. Choose the Add User option under the list of available server users.

9 Setup a new user (cont.) In the section titled Login Information - type in a username, localhost and a password in the fields as shown. Optionally you can press the Generate button to create a random password for you.

10 Setup a new user (cont.)

11 Edit Privileges After the user is created, you can see it listed on the Users page. Click Edit Privileges to assign access to a specific database. Once again leave the Global Privileges section BLANK. Scroll down to the section titled Database-Specific Privileges. Username = “root” Password = “”

12 Edit Privileges (Cont.) Choose the database you want the user to be able to access from the list, and click GO.

13 Edit Privileges (Cont.) The following permissions are recommended for compatibility with most modern web-based software applications.

14 What is SQL?  Structured Query Language  Allows database operations  Retrieve data  Modify data  Insert new data  Remove data  Create and modify tables

15 MySQL – Database - Tables  A MySQL server is capable of storing many databases  A database is generally made of a collection of related tables  Each table will have a name that is unique within the database  A table (table name - student) contains records with data StudentIDForenameSurnameLevel AK123900TrevorAdams1 AB340948BobbyRatchet3 AC234887JohanDoex2

16 MySQL - Queries  A query performed on a database can result in data or some kind of status  Queries can come in the following forms: SELECT – extracting data UPDATE – updates data DELETE – deletes data INSERT – inserts data  SELECT StudentID FROM Student StudentID AK123900 AB340948 AC234887 StudentIDForenameSurnameLevel AK123900TrevorAdams1 AB340948BobbyRatchet3 AC234887JohanDoex2

17 Connecting to databases  One of the most common tasks when working with dynamic webpages is connecting to a database which holds the content of the page  PHP has several libraries that allows for communication with many different databases  We will be using the mysqli library  Most functions start with mysqli_ and then the name of the function

18 Opening a connection  You will need: The address to the database server A username with privileges to access the table you require The username associated password The name of the database you are connecting  mysqli_connect(…..): returns a link to the host using username and password authentication. //Connect to server and database $conn = mysqli_connect("localhost", “username", “password", “database");

19 Executing Queries  mysqli_query(link, query) is used to run a query on the database server. Returns a result object. $result = mysqli_query ( $conn, “SELECT * FROM student” );  Queries can be constructed as strings and then the string variable can be used on the mysqli_query command: $my_query = "SELECT * FROM student"; $result = mysqli_query ( $link, $query );

20 Working with the result from the query  You can think of a result set as the TABLE that holds the result  You need to read the rows from that table individually using mysqli_fetch_array  Using this function, the array returned can be numerically indexed or associative! or numerically indexed and associate $row = mysqli_fetch_array($result, MYSQLI_NUM); // $row[0], $row[1] $row = mysqli_fetch_array($result, MYSQLI_ASSOC); // $row[“StudentID"], $row[“Forename"] $row = mysqli_fetch_array($result, MYSQLI_BOTH); // $row[0], $row[“Forename"]  If there are no more rows, the function returns FALSE StudentIDForenameSurnameLevel AK123900TrevorAdams1 $row

21 Keeping it tidy  Once you have completed your work with the database, there are two things you should do: Free the results Close the connection to the server.  Freeing the results can be optional: On closing the connection, the results are automatically freed  If you are planning to run further queries on the same connection, it is good practice to free the previous result set. mysqli_free_result($result);  To close the connection you use mysqli_close($conn);

22 Working with databases – key steps  Design the DB – Create the DB on PHPMyAdmin  Design and Create the HTML  Create a connection  Select the table  Run the query  Select/Insert/Delete other SQL associated tasks  Display Results as needed  Close the connection

23


Download ppt "COM621: Advanced Interactive Web Development Lecture 10 PHP and MySQL."

Similar presentations


Ads by Google