Web Units Lec 4 - PHP & MySQL v2(1)-2
Embed Size (px)
Transcript of Web Units Lec 4 - PHP & MySQL v2(1)-2
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
1/51
www.bournemouth.ac.uk
WP/WTI Lecture 4:PHP & MySQL (Part 1 o !"
Paul Albinson
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
2/51
www.bournemouth.ac.uk
#ut$%ne
n %ntro'uct%on to MySQL
MySQL PI
)%n* our MySQL er+er #rac$e +. MySQL
PHP an' MySQL
#,erat%ona$ $o*%c -) o,erat%on
ata retr%e+a$
0rror han'$%n*
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
3/51
www.bournemouth.ac.uk
An Introduction to MySQL
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
4/51
www.bournemouth.ac.uk
MySQL
MySQL % the mot ,o,u$ar o,enource re$at%ona$ 'atabae
It ,r%mar%$y com,ete a*a%ntPot*reSQL %n the o,en ource MSmarket
#wne' an' ma%nta%ne' by #rac$e (but
t%$$ ree2" It % at3 re$%ab$e3 ca$ab$e3 an' eay to
ue.
It % +ery ,o,u$ar an' % ue' %n an'
Pronounced
My S Q L
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
5/51
www.bournemouth.ac.uk
MySQL 0'%t%on
MySQL -ommun%ty 0'%t%on 8 9ree2 ece$$ent MS w%th a$$ the key
eature mot uer reu%re but %t ha nocutomer u,,ortThere % howe+er 'ocumentat%on an' a uer
orum
It % actua$$y better than the tan'ar'commerc%a$ +er%on
-ommerc%a$ +er%on 8 nnua$Subcr%,t%on Inc$u'e cutomer u ort
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
6/51
www.bournemouth.ac.uk
-ommerc%a$ ;er%on
MySQL Stan'ar' 0'%t%on (=!3>>> ayear"
a%c 'atabae eature w%th cutomeru,,ort
The ree commun%ty e'%t%on % actua$$ybetter
MySQL 0nter,r%e 0'%t%on (=53>>> ayear" Im,ro+e' enter,r%e mana*ement
eature
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
7/51www.bournemouth.ac.uk
Web rch%tecture
-$%ent
)er*ent
Ser+er
Webront7en'(,ache"
HTTP
HTTP
M%''$eware(PHP"
acken'("
API
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
8/51www.bournemouth.ac.uk
MySQL APIs
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
9/51www.bournemouth.ac.uk
PHP atabae 0ten%on
atabae eten%on are PI ue'or acce%n* a 'atabae er+er %n
PHP The $%nk between the m%''$eware an'
the backen'
There are ! ty,e o 'atabaeeten%on: Abstraction layers
Vendor-specifc extensions
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
10/51www.bournemouth.ac.uk
PI Ty,e
Abstraction layers: )n%y 'atabae %nteract%on o that co'e
ocue on 'atabae unct%on (uery3%nert3 'e$ete etc." rather than %n'%+%'ua$MS.
Th% make 'atabae co'e more @e%b$e3
Aut chan*e the conB*urat%on to ue a'%
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
11/51www.bournemouth.ac.uk
MySQL PI
PHP ha C '%
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
12/51www.bournemouth.ac.uk
My$% +. P#
Green = Advantae over other option
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
13/51
www.bournemouth.ac.uk
My$% +. P#
%or no& &e'll start &it( ys!li our work o ar ha been ,roce'ura$ an' amy$%E ,roce'ura$ unct%onE are ea%er or
be*%nner than P# weE$$ ue them or now. P# w%$$be con%'ere' or WP net emeter.
More com,ar%on: htt,://,h,.net/manua$/en/my$%no.a,%.choo%n*.,h,
htt,://co'e.tut,$u.com/tutor%a$/,'o7+7my$%7wh%ch7hou$'7you7ue77net7!4>5F
More 'eta%$ on u%n* P#:
htt,://www.%te,o%nt.com/re7%ntro'uc%n*7,'o7the7r%*ht7way7to7acce7'atabae7%n7,h,
http://www.sitepoint.com/re-introducing-pdo-the-right-way-to-access-databases-in-php/http://php.net/manual/en/mysqlinfo.api.choosing.phphttp://code.tutsplus.com/tutorials/pdo-vs-mysqli-which-should-you-use--net-24059http://code.tutsplus.com/tutorials/pdo-vs-mysqli-which-should-you-use--net-24059http://code.tutsplus.com/tutorials/pdo-vs-mysqli-which-should-you-use--net-24059http://www.sitepoint.com/re-introducing-pdo-the-right-way-to-access-databases-in-php/http://www.sitepoint.com/re-introducing-pdo-the-right-way-to-access-databases-in-php/http://www.sitepoint.com/re-introducing-pdo-the-right-way-to-access-databases-in-php/http://www.sitepoint.com/re-introducing-pdo-the-right-way-to-access-databases-in-php/http://www.sitepoint.com/re-introducing-pdo-the-right-way-to-access-databases-in-php/http://code.tutsplus.com/tutorials/pdo-vs-mysqli-which-should-you-use--net-24059http://code.tutsplus.com/tutorials/pdo-vs-mysqli-which-should-you-use--net-24059http://code.tutsplus.com/tutorials/pdo-vs-mysqli-which-should-you-use--net-24059http://php.net/manual/en/mysqlinfo.api.choosing.phphttp://php.net/manual/en/mysqlinfo.api.choosing.phphttp://php.net/manual/en/mysqlinfo.api.choosing.php -
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
14/51
www.bournemouth.ac.uk
Usin) #ur MySQL Ser*er
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
15/51
www.bournemouth.ac.uk
-onnect%on 'eta%$
To ue our MySQL er+er (a+a%$ab$eon ?au'a" you nee' the o$$ow%n*
'eta%$: Hot: 1!6.>.>.1
)ername: Gour re*u$ar uername e.*.%6654
Pawor': See your MGSQL70M0 B$e See the MGSQL70M0 B$e %n your home
'%rectory (/home/% 7 note % re,reentyour uername"
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
16/51
www.bournemouth.ac.uk
Mo'%y%n* your 'atabae
Gou can mo'%y your 'atabae: ;%a co'e 8 Ty,%ca$$y SQL w%th%n a PHP
cr%,t ;%a the ,h,My'm%n 'atabae
mana*ement web a,,$%cat%on See htt,://
*au'a.bournemouth.ac.uk/*au'a'ba Dote th% current$y on$y work %n%'e )
;%a MySQL workbench (work anywhere"
?u%'e on u%n* ,h,My'm%n an'MySQL workbench are ,ro+%'e' on My)
https://gauda.bournemouth.ac.uk/gaudadbahttps://gauda.bournemouth.ac.uk/gaudadbahttps://gauda.bournemouth.ac.uk/gaudadbahttps://gauda.bournemouth.ac.uk/gaudadba -
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
17/51
www.bournemouth.ac.uk
#racle Vs$ MySQL
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
18/51
www.bournemouth.ac.uk
#rac$e +. MySQL
The ,ur,oe % not abouthe$,%n* youchooe the r%*ht 'atabae to ue a the
two are bu%$t or '%
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
19/51
www.bournemouth.ac.uk
ata uthent%cat%on
It % a ,roce ue' to +er%y the%'ent%ty o a 'atabae uer %n or'er to
,erm%t them to acce3 ,roce ora$ter 'ata.
MySQLE %m,$ementat%on: ?rant tab$e are ue' to kee, track o
uer an' the ,r%+%$e*e that they canha+e.
Locat%on can be checke' a$on* w%th the
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
20/51
www.bournemouth.ac.uk
Schema
'atabae chema conta%n the 'eBn%t%on othe tab$e3 +%ew3 %n'ee3 uer3 contra%nt3tore' ,roce'ure3 tr%**er3 an' other
'atabae7,ec%Bc obAect. It can be con%'ere' a the hea'erJ B$e o a
'atabae wh%ch 'ecr%be %t tructure (b$ue,r%nt".
common un'ertan'%n* % that a databasein MySQL is a sc(ea in #racle. When u%n* a 'atabae3 you ue chema %n #rac$e
wh%$e you ue 'atabae %n MySQL.
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
21/51
www.bournemouth.ac.uk
ata Ty,e
oth MySQL an' #rac$e u,,ort threema%n ty,e: numeric3 date and
time3 an' string.
The ma%n '%"3 9L#T(!4".
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
22/51
www.bournemouth.ac.uk
ata Stora*e -once,t
In #rac$e3 tablespaces,ro+%'e $o*%ca$tora*e ,ace that $%nk a 'atabae to the,hy%ca$ 'ata B$e.
In MySQL3 a stora)e en)ine% ue' to 'ea$w%th 'ata tora*e an' retr%e+a$.
9or the 'eau$t tora*e en*%ne MyISM3 each'atabae % a '%rectory w%th%n the MySQL 'ata'%rectory where each tab$e o the 'atabae %tore' %n a e,arate et o B$e w%th%n the'%rectory.
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
23/51
www.bournemouth.ac.uk
SQL Quer%e
oth #rac$e an' MySQL are MSwh%ch u,,ort SQL uer%e.
Howe+er3 they ha+e di+erent%m,$ementat%on o SQL ynta. -once,tua$$y3 they are the ame but
,ract%ca$$y they are '%
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
24/51
www.bournemouth.ac.uk
SQL Synta 0am,$e
etr%e+e the Brt 5 row rom a'atabae tab$e
MySQLSELECT columnsFROM tableORDER BY keyASC LIMIT5;
#racleSELECT * FROM (SELECT columnsFROMtable ORDER BY keyASC) WHERE ROWNUM
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
25/51
www.bournemouth.ac.uk
P,P and MySQL
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
26/51
www.bournemouth.ac.uk
atabae #,erat%on Lo*%c
-onnect to
the er+er
Se$ect a
'atabae
Subm%t a
uery
etr%e+e'ata
%connectrom theer+er
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
27/51
www.bournemouth.ac.uk
MySQL% Ma,,%n*
#,erat%onLo*%c
my$% 0ten%on
Ser+er connect%on mysqli_connect()
atabae e$ect%on mysqli_select_db()
Query ubm%%on mysqli_query()mysqli_multi_query()
ata retr%e+a$(etch row or row"
mysqli_fetch_assoc()
mysqli_fetch_array()
mysqli_fetch_all()
n' other mysqli_fetch_ +ar%at%on
Memory ree u, mysqli_free_result()
Ser+er '%connect%on mysqli_close()
0rror han'$%n* mysqli_error()
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
28/51
www.bournemouth.ac.uk
Ser+er -onnect%on
Synta:
0u%+a$ent MySQL comman':
0am,$e:
9or our er+er the 'atabae name % the ame a theuername3 %n or'%nary c%rcumtance %t wou$' be'%
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
29/51
www.bournemouth.ac.uk
atabae Se$ect%on
Synta:
0u%+a$ent SQL ynta: 0am,$e:
!"## = mysqli_!"##e!$(%&'&%+%i&',-5.%+
%/0ss1"r2%);
mysqli_sele!$_2(!"##+ %my2%);
mysqli_sele!$_2(connection+dbname);
6SE dbname;
)ote as you can speci*y the database +hen connectin to the
server usin mysqli_!"##e!$()the only time you +ouldneed to use this is to chane databases a*ter connectin.
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
30/51
www.bournemouth.ac.uk
Ser+er %connect%on
Synta:
0u%+a$ent MySQL comman':
0am,$e:
!"## = mysqli_!"##e!$(%&'&%+%i&',-5.%+
%/0ss1"r2%+%i&',-5.%);
mysqli_!l"se(!"##);
mysqli_!l"se(dbconnection);
e7i$
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
31/51
www.bournemouth.ac.uk
-) #,erat%on
-) tan' or reate3 .ead3 Updatean' Delete%n re$at%ona$ 'atabaea,,$%cat%on.
It reer to INS/.01./A0/3 S/L/03UPDA0/an' D/L/0/1D.#PSQL
tatement.
-) o,erat%on are %m,$emente' a SQL!ueries1coands.
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
32/51
www.bournemouth.ac.uk
-) #,erat%on %n PHP
SQL uer%e are *enera$$y create' a strin)s%nPHP
They are eecute' on the MySQL er+er by u%n*mysqli_query()unct%on.
y 'eau$t3 uery reu$t are returne' a a result
set(mysqli_resul$obAect".
They can be then retr%e+e' by u%n* unct%on'eBne' %n mysqli_resul$c$a.
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
33/51
www.bournemouth.ac.uk
-): -reate Tab$e
SQL ynta (ba%c":
0am,$e:
CREATE TABLE table (coltype);
query = 8CREATE TABLE 9F :OT E9STS/r"2u!$s (
/_i2 9:T :OT :6LL A6TO_9:CREME:T+
R9MARY EY (/_i2)+/_#0me >ARC?AR('55)+/_s$"[email protected] 9:T
)8;
resul$ = mysqli_query(!"##+ query);
,K%' ,Kname ,Ktock
IDT ;-H (!55" IDT
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
34/51
www.bournemouth.ac.uk
-): Inert ata
SQL ynta (ba%c":
0am,$e:
9:SERT 9:TO table(col1,col2)>AL6ES (value1+ value2);
query = 89:SERT 9:TO /r"2u!$s (/_i2+/_#0me+ /_s$"[email protected]) >AL6ES (:6LL+ %%+
);8;resul$ = mysqli_query(!"##+query);,K%' ,Kname ,Ktock
> >
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
35/51
www.bournemouth.ac.uk
-): Se$ect ata
SQL ynta (ba%c":
0am,$e:
SELECT * FROM table?ERE col=
%value
%;
query = 8SELECT * FROM /r"2u!$s?ERE /_s$"[email protected] = ;8;
resul$ = mysqli_query(!"##+query);
,K%' ,Kname ,Ktock
0 f 0
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
36/51
www.bournemouth.ac.uk
-): ),'ate ata
SQL ynta (ba%c":
0am,$e:
6DATE tableSET col=%value%?ERE col= %value%;
query = 86DATE /r"2u!$s SET /_s$"[email protected] ='& ?ERE /_s$"[email protected] = ;8;
resul$ = mysqli_query(!"##+ query);
,K%' ,Kname ,Ktock
> 21
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
37/51
www.bournemouth.ac.uk
-): e$ete ata
SQL ynta (ba%c":
0am,$e:
DELETE FROM table?ERE col=
%value%;
query = 8DELETE FROM /r"2u!$
?ERE /_s$"[email protected] = ;8;
resul$ = mysqli_query(!"##+query);
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
38/51
www.bournemouth.ac.uk
-): ro,/e$ete a Tab$e
SQL ynta (ba%c":
0am,$e:
DRO TABLE 9F E9STS
table;
query = 8DRO TABLE 9F E9STS/r"2u!$s;8;
resul$ = mysqli_query(!"##+ query);
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
39/51
www.bournemouth.ac.uk
ata etr%e+a$ Lo*%c %n PHP
Query reu$t are norma$$y ca$$e' data ro&s%n a 'atabae.
Thee 'ata row are returne' to PHP a aresult setwhere: 0ach 'ata row w%$$ be tore' %n an array.
The key o the array % the tab$e colunorco$umn index.
The +a$ue o the array % the data *aluecorre,on'%n* to the co$umn.
Loo,/%terat%on % ue' to *et a$$ 'ata row.
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
40/51
www.bournemouth.ac.uk
ata etr%e+a$ %a*ram
,K%' ,Kname
,Ktock
> a 1>
1 b 11
! c 1!
C ' 1C
.esult set
, a -,
- b --
c -
/ d -/
, a -, Array-
0ey , -
1alue
- b -- Array1alue
c - Array/1alue
SELECT * FROM /r"2u!$s
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
41/51
www.bournemouth.ac.uk
ata etr%e+a$ 0am,$e
?et a reu$t row a an enumerate' array:
0am,$e:
mysqli_e$!4_r"1();
query = 8SELECT * FROM /r"2u!$s?ERE /_s$"[email protected] ;8;
resul$ = mysqli_query(!"##+
query);14ile (r"1 =mysqli_e$!4_r"1(resul$))
e!4" r"1+ r"1&+ r"1';
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
42/51
www.bournemouth.ac.uk
ata etr%e+a$ 0am,$e
9etch a reu$t row a an aoc%at%+e3 anumer%c array3 or both:
0am,$e w%th numer%c array:
mysqli_e$!4_0rr0y();
query = 8SELECT /_i2+ /_s$"[email protected] /r"2u!$s ?ERE /_s$"[email protected]
;8;
resul$ = mysqli_query(!"##+ query);14ile (r"1 =mysqli_e$!4_0rr0y(resul$+ MYSHL9_:6M))
e!4" r"1+ r"1&;
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
43/51
www.bournemouth.ac.uk
ata etr%e+a$ 0am,$e
9etch a reu$t row a an aoc%at%+e3 anumer%c array3 or both:
0am,$e w%th aoc%at%+e array:
mysqli_e$!4_0rr0y();
query = 8SELECT /_i2+ /_s$"[email protected] /r"2u!$s ?ERE /_s$"[email protected]
;8;
resul$ = mysqli_query(!"##+ query);14ile (r"1 =mysqli_e$!4_0rr0y(resul$+MYSHL9_ASSOC))
e!4" r"18/_i28+ r"18/_s$"[email protected];
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
44/51
www.bournemouth.ac.uk
ata etr%e+a$ 0am,$e
9etch a reu$t row a an aoc%at%+e3 anumer%c array3 or both:
0am,$e w%th both numer%c an'aoc%at%+e:
mysqli_e$!4_0rr0y();
query = 8SELECT /_i2+ /_s$"[email protected] /r"2u!$s ?ERE /_s$"[email protected]
;8;
resul$ = mysqli_query(!"##+ query);14ile (r"1 = mysqli_e$!4_0rr0y(resul$+
MYSHL9_BOT?))
e!4" r"18/_i28+ r"1&;
G
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
45/51
www.bournemouth.ac.uk
ata etr%e+a$ 0am,$e
There are other way o etch%n*reu$t 8 See htt,://
,h,.net/manua$/en/c$a.my$%7reu$t.,h,
http://php.net/manual/en/class.mysqli-result.phphttp://php.net/manual/en/class.mysqli-result.phphttp://php.net/manual/en/class.mysqli-result.phphttp://php.net/manual/en/class.mysqli-result.phphttp://php.net/manual/en/class.mysqli-result.phphttp://php.net/manual/en/class.mysqli-result.php -
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
46/51
www.bournemouth.ac.uk
0rror Han'$%n*
-onnect%on error: mysqli_!"##e!$() It w%$$ return %ALS/% connect%n* a%$
The error can be ca,ture' u%n*mysqli_!"##e!$_err"r()
Query error: mysqli_query() It w%$$ return %ALS/% the uery a%$
The error can be ca,ture' w%th mysqli_err"r()
PHP unct%on 2ie()% norma$$y ue' when maAorerror are 'etecte' It w%$$ term%nate the current cr%,t an' e%t the ,ro*ram
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
47/51
www.bournemouth.ac.uk
0rror Han'$%n* 0am,$e
Dorma$
Shorthan'
!"## =mysqli_!"##e!$(8&'&8+8user#0me8+
8/0ss1"r28+ 828);i (I!"##) 2ie(mysqli_!"##e!$_err"r());G
e!4" 8
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
48/51
www.bournemouth.ac.uk
0rror Han'$%n* 0am,$e
eta%$e' connect%on error mea*e
!"## =mysqli_!"##e!$(8&'&8+8user#0me8+
8/0ss1"r28+ 828);
i (mysqli_!"##e!$_err"r()) e!4" 8F0ile2 $" !"##e!$ $" MySHLK
mysqli_!"##e!$_err"r();G
e!4" 8
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
49/51
www.bournemouth.ac.uk
onclusion
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
50/51
www.bournemouth.ac.uk
9urther rea'%n*
MySQL% %n PHP Manua$
MySQL 5. eerence Manua$
)$$man -ha,ter 4353 We$$%n* & Thomon -ha,ter
3F31>311
-
7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2
51/51
Pre+%ew o the net $ecture
oo2ies
The conce,t o cook%e
-) #,erat%on o -ook%e )e or cook%e