Date post: | 07-Jan-2017 |
Category: |
Software |
Upload: | mike-crabb |
View: | 1,132 times |
Download: | 1 times |
webdev@rguusing mysql in php
what we are covering:
web tier design introduction to sql rules for creating tables rules for writing sql using databases in php
web tier design
2-tier
CLIENT SERVER
3-tier
CLIENT SERVER DATABASE
CLIENT SERVER DATABASE
PRESENTATION TIER
LOGIC TIER
DATA TIER
HTML & CSS PHP MYSQL
‘Machine’ names
‘tier’ name
‘technology’ name
n-tier
benefits of n-tier
AVAILABILITY REDUNDANCY SCALABILITY PERFORMANCE
availability
IF ONE SERVER GOES DOWN THE REST STILL RUN
redundancy
BACKUPS ENSURE THAT DATA IS ALWAYS AVAILABLE
performance
LOAD BALANCING CAN MAKE SURE THAT SERVERS ARE USED OPTIMALLY
scalability
MORE SERVERS CAN BE ADDED AS AND WHEN THEY ARE NEEDED
disadvantages of n-tier
• LATENCY • INCREASED NUMBER OF HOPS • UNPREDICTABLE LINK QUALITY • NETWORK IS THE SLOWEST LINK
• COMPLEXITY • THERE IS NO MANUAL FOR WEB-
SCALE COMPUTING • FACEBOOK / REDDIT / TWITTER HAD
TO LEARN AS THEY WENT ALONG
introduction to SQL
mysql•Released in 1995 •Lots of different pronunciations
•My Ess Que Ell •My Sequel (I prefer this one)
•Most popular open source relational database management system (RDBMS
mysql•http://www.mysql.com provides more information and documentation
•Its very popular •Open source licence •Fast, reliable and easy to use •Free to use! •mySQL Server works in local and remote systems
•Used by a lot of mainstream companies •Wordpress, Drupal, other CMS •Facebook (ish)
mysqlwe will be using azure!
(this means you’ll need to log into it again…and remember your password)
mysql visualising a database
there are lots of different ways that we can use to visualise a database. No one way is the best/worst. It’s mostly down to personal preference (of who you’re working for!)
1. Tables are represented by boxes similar to this
1. Tables are represented by boxes similar to this 2. The name of the table goes in the box at the top
1. Tables are represented by boxes similar to this 2. The name of the table goes in the box at the top 3. The rows in the table go in the boxes underneath
1. Tables are represented by boxes similar to this 2. The name of the table goes in the box at the top 3. The rows in the table go in the boxes underneath 4. Some table rows have a special notation attached to
them
1. Tables are represented by boxes similar to this 2. The name of the table goes in the box at the top 3. The rows in the table go in the boxes underneath 4. Some table rows have a special notation attached to
them 1. Primary Keys are signified by an underline
1. Tables are represented by boxes similar to this 2. The name of the table goes in the box at the top 3. The rows in the table go in the boxes underneath 4. Some table rows have a special notation attached to
them 1. Primary Keys are signified by an underline 2. Foreign Keys are signified by an asterisk
• The PRIMARY KEY constraint uniquely identifies each record in a database table.
• Primary keys must contain UNIQUE values.
• A primary key column cannot contain NULL values.
primary keys
• A FOREIGN KEY in one table points to a PRIMARY KEY in another table.
foreign keys
• Reduce repeating data
• Easier to change one thing than to change lots of things
reasons for linking tables
One to One
One to Many
Many to One
Many to Many
types of relationships (links)
One to One
One to Many
Many to One
Many to Many
types of relationships (links)
Rules for creating tables in
mysql
this is a fairly standard table design
myFirstTable
email username password
CREATE TABLE `myFirstTable` ( `email` VARCHAR( 50 ) NOT NULL , `username` VARCHAR( 25 ) NOT NULL , `password` VARCHAR( 255 ) NOT NULL , PRIMARY KEY (`email`) )
you should know how to turn
myFirstTable
email username password
CREATE TABLE `myFirstTable` ( `email` VARCHAR( 50 ) NOT NULL , `username` VARCHAR( 25 ) NOT NULL , `password` VARCHAR( 255 ) NOT NULL , PRIMARY KEY (`email`) )
CREATE TABLE `myFirstTable` ( `email` VARCHAR( 50 ) NOT NULL , `username` VARCHAR( 25 ) NOT NULL , `password` VARCHAR( 255 ) NOT NULL , PRIMARY KEY (`email`) )
Golden Rule All names must be presented
‘likeThis’
CREATE TABLE `myFirstTable` ( `email` VARCHAR( 50 ) NOT NULL , `username` VARCHAR( 25 ) NOT NULL , `password` VARCHAR( 255 ) NOT NULL , PRIMARY KEY (`email`) )
1. When we are creating a table we use the keywords create table followed by the name that we want to call it
Golden Rule All names must be presented
‘likeThis’
CREATE TABLE `myFirstTable` ( `email` VARCHAR( 50 ) NOT NULL , `username` VARCHAR( 25 ) NOT NULL , `password` VARCHAR( 255 ) NOT NULL , PRIMARY KEY (`email`) )
1. When we are creating a table we use the keywords create table followed by the name that we want to call it
2. Any row that we want to create in the table are then placed between brackets
Golden Rule All names must be presented
‘likeThis’
CREATE TABLE `myFirstTable` ( `email` VARCHAR( 50 ) NOT NULL , `username` VARCHAR( 25 ) NOT NULL , `password` VARCHAR( 255 ) NOT NULL , PRIMARY KEY (`email`) )
1. When we are creating a table we use the keywords create table followed by the name that we want to call it
2. Any row that we want to create in the table are then placed between brackets
3. Each row that we want to create has its own line
Golden Rule All names must be presented
‘likeThis’
CREATE TABLE `myFirstTable` ( `email` VARCHAR( 50 ) NOT NULL , `username` VARCHAR( 25 ) NOT NULL , `password` VARCHAR( 255 ) NOT NULL , PRIMARY KEY (`email`) )
1. When we are creating a table we use the keywords create table followed by the name that we want to call it
2. Any row that we want to create in the table are then placed between brackets
3. Each row that we want to create has its own line 1. Start with the name of the row
Golden Rule All names must be presented
‘likeThis’
CREATE TABLE `myFirstTable` ( `email` VARCHAR( 50 ) NOT NULL , `username` VARCHAR( 25 ) NOT NULL , `password` VARCHAR( 255 ) NOT NULL , PRIMARY KEY (`email`) )
1. When we are creating a table we use the keywords create table followed by the name that we want to call it
2. Any row that we want to create in the table are then placed between brackets
3. Each row that we want to create has its own line 1. Start with the name of the row 2. Put in the data type
Golden Rule All names must be presented
‘likeThis’
CREATE TABLE `myFirstTable` ( `email` VARCHAR( 50 ) NOT NULL , `username` VARCHAR( 25 ) NOT NULL , `password` VARCHAR( 255 ) NOT NULL , PRIMARY KEY (`email`) )
1. When we are creating a table we use the keywords create table followed by the name that we want to call it
2. Any row that we want to create in the table are then placed between brackets
3. Each row that we want to create has its own line 1. Start with the name of the row 2. Put in the data type 3. Define the length of the data type in brackets
Golden Rule All names must be presented
‘likeThis’
CREATE TABLE `myFirstTable` ( `email` VARCHAR( 50 ) NOT NULL , `username` VARCHAR( 25 ) NOT NULL , `password` VARCHAR( 255 ) NOT NULL , PRIMARY KEY (`email`) )
1. When we are creating a table we use the keywords create table followed by the name that we want to call it
2. Any row that we want to create in the table are then placed between brackets
3. Each row that we want to create has its own line 1. Start with the name of the row 2. Put in the data type 3. Define the length of the data type in brackets 4. Then think about null/not null
Golden Rule All names must be presented
‘likeThis’
CREATE TABLE `myFirstTable` ( `email` VARCHAR( 50 ) NOT NULL , `username` VARCHAR( 25 ) NOT NULL , `password` VARCHAR( 255 ) NOT NULL , PRIMARY KEY (`email`) )
1. When we are creating a table we use the keywords create table followed by the name that we want to call it
2. Any row that we want to create in the table are then placed between brackets
3. Each row that we want to create has its own line 1. Start with the name of the row 2. Put in the data type 3. Define the length of the data type in brackets 4. Then think about null/not null
4. Finally think about primary keys
Golden Rule All names must be presented
‘likeThis’
rules for writing sql
we’ll use this database as an example
(sorry if you don’t like marvel)
sql selectWhen we want to get information from the database we use a select statement
SELECT columnName, columnName FROM tableName;
sql select *If we want to select everything in a table we use the * character
SELECT * FROM tableName;
sql whereThe where keyword can be used to filter the information that we get back from the database
SELECT columnName, columnName FROM tableName WHERE condition;
sql whereLots of different operators that we can use for this
= equals <> does not equal > greater than < less than >= greater than or equal to <= less than or equal to LIKE search for a pattern
sql and / orCan use AND and OR to further filter the data and add more conditions to our search
SELECT columnName, columnName FROM tableName WHERE condition AND condition;
sql insertInsert can be used to put information into the database
INSERT INTO tableName (column1, column2, column3) VALUES (value1, value2, value3)
goes into our database. Note that I didn’t put in the ID (primary key) this was auto generated for me
sql updateUpdate can be used to edit data that exists in our database
UPDATE tableName SET column1=value1, column2=value2 WHERE condition
sql deleteDelete is used to remove information from our database
DELETE FROM tableName WHERE condition
using databases in
php
CLIENT SERVER DATABASE
WE WILL BE USING A MYSQL DATABASE HOSTED BY CLEARDB
not this one!
finding your connection details
connecting your database to php
THERE ARE A NUMBER OF DIFFERENT WAYS THAT YOU CAN USE TO CONNECT A DATABASE TO PHP
WE ARE GOING TO BE USING MYSQLI
connecting your database to phpTHERE ARE A NUMBER OF DIFFERENT WAYS THAT YOU CAN USE TO CONNECT A DATABASE TO PHP
WE ARE GOING TO BE USING MYSQLI
THE BEST WAY TO USE THIS IS TO CREATE A NEW PHP FILE CALLED SOMETHING LIKE DBCONNECT.PHP
dbconnect.php
HINT: IF YOU COPY AND PASTE THE TEXT FROM THIS SLIDE YOU CODE MAY BREAK AND I WILL LAUGH.
WRITE THIS OUT YOURSELF!
<?php
define(‘DB_SERVER’, ‘servername’); define(‘DB_USERNAME’, ‘username’); define(‘DB_PASSWORD’, ‘password’); define(‘DB_DATABASE’, ‘database’);
$db = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
using dbconnect.phpANY TIME THAT WE WANT TO USE OUR DATABASE WE INCLUDE THE FOLLOWING LINE AT THE TOP OF OUR PHP PAGE:
include(“dbconnect.php”);
AND CAN THEN CALL $DB ANY TIME THAT WE WANT TO USE IT
$sql = “SELECT * from marvel movies”;
$result = mysqli_query($db, $sql);
$row = mysql_fetch_array($result, MYSQLI_ASSOC);
//SETS OUR SQL STATEMENT
//RUNS THE SQL STATEMENT ON OUR DATABASE
//FETCHES THE RESULT AS AN ARRAY
recapweb tier design introduction to sql rules for creating tables rules for writing sql using databases in php