Website Development Working with Databases. What you will achieve today! Connecting to mySql Creating tables in mySql Saving data on a server using mySql.

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

PHP II Interacting with Database Data. The whole idea of a database-driven website is to enable the content of the site to reside in a database, and to.
Website Development Working with Databases. What you will achieve today! Connecting to mySql Creating tables in mySql Saving data on a server using mySql.
What is MySQL? MySQL is a relational database management system (A relational database stores data in separate tables rather than putting all the data.
Let’s try Oracle. Accessing Oracle The Oracle system, like the SQL Server system, is client / server. For SQL Server, –the client is the Query Analyser.
Website Development Registering Users – Introducing Cookies.
PHP (2) – Functions, Arrays, Databases, and sessions.
Website Development Working with MySQL. What you will achieve today! Connecting to mySql Creating tables in mySql Saving data on a server using mySql.
Dynamic Web site With PHP and MySQL. MySQL The combination of MySQL database and PHP scripting language is optimum for building dynamic websites. MySQL.
Lecture 3 – Data Storage with XML+AJAX and MySQL+socket.io
Deleting and Updating Records in MySQL using PHP Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Advanced Web 2012 Lecture 4 Sean Costain PHP Sean Costain 2012 What is PHP? PHP is a widely-used general-purpose scripting language that is especially.
CSCI 6962: Server-side Design and Programming
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Server-side Scripting Powering the webs favourite services.
ASP.NET Programming with C# and SQL Server First Edition
Advanced Database Management System Lab no. 11. SQL Commands (for MySQL) –Update –Replace –Delete.
Chapter 7 PHP Interacts with Ms. Access (Open DataBase Connectivity (ODBC))
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
INTERNET APPLICATION DEVELOPMENT PRACTICAL ON CONNECTING TO MYSQL.
MySQL + PHP.  Introduction Before you actually start building your database scripts, you must have a database to place information into and read it from.
1 MySQL and phpMyAdmin. 2 Navigate to and log on (username: pmadmin)
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
PHP meets MySQL.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Introduction to MySQL Lab no. 10 Advance Database Management System.
Installing and Using MySQL and phpMyAdmin. Last Time... Installing Apache server Installing PHP Running basic PHP scripts on the server Not necessary.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
SQL 101 for Web Developers 14 November What is a database and why have one? Tables, relationships, normalization SQL – What SQL is and isn’t – CRUD:
PHP and MySQL CS How Web Site Architectures Work  User’s browser sends HTTP request.  The request may be a form where the action is to call PHP.
Lecture 10 – MYSQL and PHP (Part 2)
Chapter 10: The Data Tier We discuss back-end data storage for Web applications, relational data, and using the MySQL database server for back-end storage.
ASP.NET The Clock Project. The ASP.NET Clock Project The ASP.NET Clock Project is the topic of Chapter 23. By completing the clock project, you will learn.
Most information comes from Chapter 3, MySQL Tutorial: 1 MySQL: Part.
HTML, PHP, and MySQL: Putting It All Together. Making a Form Input tags Types: “text” “radio” “checkboxes” “submit”
THE WEBMASTERS: SENG + WAVERING.  On account of construction, we will be having class in room 1248 next week.
NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
MySQL. Is a SQL (Structured Query Language) database server. Can be accessed using PHP with embedded SQL Queries Supports Large DB’s, 60,000 tables with.
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
NSF DUE ; Wen M. Andrews J. Sargeant Reynolds Community College Richmond, Virginia.
CHAPTER 10 PHP MySQL Database
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 1: Introduction to IS2803 Rob Gleasure
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
MySQL MySQL and PHP – interacting with a database.
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
Programming for the Web MySQL Command Line Using PHP with MySQL Dónal Mulligan BSc MA
COM621: Advanced Interactive Web Development Lecture 10 PHP and MySQL.
 MySQL is a database system used on the web  MySQL is a database system that runs on a server  MySQL is ideal for both small and large applications.
Web Systems & Technologies
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Introduction to Web programming
Database application MySQL Database and PhpMyAdmin
Introduction to Web programming
BASIC PHP and MYSQL Edward S. Flores.
Website Development Basics with PHP MySQL
ISC440: Web Programming 2 Server-side Scripting PHP 3
Chapter 8 Working with Databases and MySQL
Developing a Model-View-Controller Component for Joomla Part 3
Web Programming Language
Tutorial 6 PHP & MySQL Li Xu
Introduction to Web programming
Presentation transcript:

Website Development Working with Databases

What you will achieve today! Connecting to mySql Creating tables in mySql Saving data on a server using mySql Getting data from the server using mySql

: Customer browser request service access page interpret set data present html return html get data databasescripting language web server Reminder of the general process today’s emphasis

The database A relational database is made up of tables (sometimes known as relations) Tables are made up of rows (records) and columns (fields, attributes) Table structure is described in terms of its columns (fields, attributes) E.g. cars (make, model, doors, fuel, cost) Tables are usually linked to other tables via relationships

Outline MySQL PHPMyAdmin Simple database communication using PHP

MySQL is a relational database management system. A relational database stores data in separate tables rather than putting all the data in one big storeroom. This adds speed and flexibility. The tables are linked by defined relations making it possible to combine data from several tables on request. The SQL part of MySQL stands for "Structured Query Language" - the most common standardized language used to access databases.

MySQL Lightweight Easy to administer Runs on low power machines Great for development Not quite there when compared to Oracle, MS SQL Server or IBM DB2 but coming along fast ( )

Installing on your machine If you are going to work at home with PHP/MySQL I recommend one of the distributed packages for web development The one we use at uni is called Wamp and is available from There are other alternatives

GUI Most common is phpMyAdmin Hermes link is Normal username and password Let’s have a look at it…

The Four SQL Commands SELECT INSERT UPDATE DELETE These only affect values in the database, not the structure, but once you know these four you are set for life* * not guaranteed

SELECT The example table is called "Cars" Each record is made up of data describing ID Make Model Doors Fuel and Cost The SQL to retrieve all rows and all columns is:- SELECT * FROM Cars The * indicates that all columns from ‘Cars’ are to be included. This could have been written as:- SELECT ID Make Model Doors Fuel and Cost FROM Cars

Query result The code for all (*) would result in the following being returned SELECT *FROM Cars 1 Audi A4 4 Petrol £19,300 2 Audi A3 2 Petrol £16,250 3 Audi A4 Avant 5 Petrol £20,400 4 Audi A6 4 Diesel £28,880..etc

More precise SELECT Model FROM Cars A4 A3 A4 Avant A6..etc

More precise SELECT Model,Fuel FROM Cars A4 Petrol A3 Petrol A4 Avant Petrol A6 Diesel..etc

Conditional SQL ? We can use WHERE, along with = <> >= <= BETWEEN and LIKE, to narrow down our search: SELECT Model FROM Cars WHERE Fuel='Diesel' A6 Corolla Corolla 330..etc

Conditional query SELECT Model,Fuel FROM Cars WHERE cost<10,000 Corolla Petrol Corolla Petrol Corolla Petrol Corolla Diesel Corolla Petrol..etc

Wild cards and Like SELECT Model FROM Cars WHERE Model LIKE '3%' Coupe 330

Conditional logic SELECT Model,Fuel FROM Cars WHERE cost BETWEEN £20,000 AND £25,000 A4 Avant Petrol

Multiple conditions With AND and OR we are not limited to one condition: SELECT Model,Fuel,Cost FROM Cars WHERE cost<20,000 AND Model = 'Audi' A3 Petrol £16,250

The IN Keyword This is a neat way of doing multiple ORs. For example, suppose we wanted a listing of all rows where the make is “AUDI” or “ALPHA” We could obviously do this with an OR but we can also do it with IN. SELECT * FROM Cars WHERE MAKE IN (“AUDI”, “ALPHA”) You can have as many alternatives as you like in the brackets above.

The BETWEEN Keyword We want to list all rows where cost is greater than or equal to and less than or equal to We could do this as SELECT * FROM Cars WHERE cost >= AND cost <= An alternative way of writing this is to use the BETWEEN keyword. SELECT * FROM Cars WHERE cost BETWEEN AND 20000

INSERT INSERT INTO table_name VALUES (value1, value2,....) Or INSERT INTO table_name (column1, column2,...) VALUES (value1, value2,....)

UPDATE UPDATE table_name SET column_name = new_value WHERE column_name = some_value Multiple values with a comma seperated UPDATE table_name SET column_name = new_value, column_name2 = new_value2 WHERE column_name = some_value

DELETE DELETE FROM table_name WHERE column_name = some_value

Tutorial Go to and ‘play’ with SELECT sql Try the examples on the page and create a few of your own Use phpMyAdmin at to ‘play’ with a mysql database

Accessing MySQL from PHP

Connecting to PHP We can now write SQL and setup our MySQL database But we want to be able to talk to the database using PHP Its easy! For now simple table editing (update data, delete etc), later database editing (create, dump tables etc)

Creating the connection mysql_connect($hostname, $user, $password);

Then choose a database… mysql_select_db(‘carsDB’);

Now ask it a question… $result = mysql_query(“SELECT * FROM Cars”);

Lets see how many results came back… $num = mysql_num_rows($result);

Now output it to the screen… Options for dealing with $result mysql_fetch_row - returns row as enumerated array mysql_fetch_array – returns row as an associative array mysql_fetch_row used to be fastest, but mysql_fetch_array is the preferred favourite now

Output to the screen… while($row = mysql_fetch_array($result)) { print($row[‘Make’]. $row[‘Model’]. $row[‘Price’]. “ ”); }

Putting data into the database Example uses cars database MakeModelPrice SmartRoadster14000 Rover216si1800 RobinReliant10

Psuedo code Take inputs from form Write sql string including inputted data Connect to database Run sql command string Check to see if it was successful

The form Example html Make: Model: Price:

Get data from the form $Make = $_POST['Make']; $Model = $_POST['Model']; $Price = $_POST['Price'];

Create the sql string $sql = "INSERT INTO cars (Make,Model,Price) VALUES ('$Make','$Model',$Price)";

Connect to mysql and the database mysql_connect('localhost','root') or die("cant talk to mysql"); mysql_select_db(‘cars') or die("cant connect to the database");

Run the mysql command $result = mysql_query($sql);

Check to make sure it was successful if(mysql_affected_rows() == 1) { print("yay, it worked"); } else { print("something went horribly wrong!"); }

Why 2 files? Sometimes we need everything to be done in one file E.g. I fill in a form, submit to another page, but something went wrong, now I have to go back and fill in everything again! To solve this do all the processing in one page where the form submits to itself.

<?php $Make = $_POST['Make']; $Model = $_POST['Model']; $Price = $_POST['Price']; $submit = $_POST['submit']; $success = 0; if ($submit == "Save" && $Make != NULL && $Model != NULL && $Price != NULL) { $sql = "INSERT INTO cars (Make,Model,Price) VALUES ('". $Make. "','". $Model. "',". $Price. ")"; mysql_connect('localhost','root') or die("cant talk to mysql"); mysql_select_db(‘cars') or die("cant connect to the database"); $result = mysql_query($sql); if(mysql_affected_rows() == 1){ print("yay, it worked"); $success = 1; } else{print("something went horribly wrong!");} } if($success != 1) { ?> "> Make: " /> Model: " /> Price: " /> <?php } ?>

Editing data in the database Pseudo code Find the data to be changed from the database Output the data into an editable area (e.g. a form) Resubmit the data Update the database

Connect to the database mysql_connect('localhost','root') or die("cant talk to mysql"); mysql_select_db(‘cars') or die("cant connect to the database"); $sql = "SELECT * FROM cars"; $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { print("$row[0] $row[1] - edit "); }

$Make = $_GET['Make']; $Model = $_GET['Model']; $Price = $_GET['Price']; $success = 0; $submit = $_GET['submit']; if($submit == "Update" && $Make != NULL && $Model != NULL && $Price != NULL) { $sql = "UPDATE cars SET Make='$Make', Model='$Model', Price=$Price WHERE Make='$Make' AND Model='$Model'"; mysql_connect('localhost','root') or die("cant talk to mysql"); mysql_select_db(‘cars') or die("cant connect to the database"); $result = mysql_query($sql); if(mysql_affected_rows() == 1) { print("yay, it worked"); $success = 1; } else{ print("something went horribly wrong!");} } if($success != 1) { ?> "> Make: " /> Model: " /> Price: " /> <?php }

A whole system… Admin area would need A menu A page for viewing data A page for adding new data A page for editing existing data A page for deleting existing data

Tutorial Exercise From last week… Make a simple database in phpMyAdmin Add a table, e.g. cars and populate with data Advanced users can write php to create the tables Write a simple php file to access database and output results to the screen ‘tart up’ your output page to look more readable Plus new… Create new files for editing and adding data Create a little menu (i.e. a mini admin site)

If you can do all that You can do the PHP part of the coursework

Tutorial Make a simple database in phpMyAdmin Add a table, e.g. Guestbook and populate with data Write a simple php file to access the database and output results to the screen Tidy up your output page to look more readable