Download - Web Units Lec 4 - PHP & MySQL v2(1)-2

Transcript
Page 1: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-2 1/51

www.bournemouth.ac.uk

WP/WTI Lecture 4:PHP & MySQL (Part 1 o !"

Paul Albinson

[email protected]

Page 2: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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*

Page 3: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-2 3/51

www.bournemouth.ac.uk

An Introduction to MySQL

Page 4: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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

Page 5: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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,,ort• There % 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

Page 6: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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

Page 7: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-2 7/51www.bournemouth.ac.uk

Web rch%tecture

-$%ent

)er*ent

Ser+er

Webront7en'(,ache"

HTTP

HTTP

M%''$eware(PHP"

acken'("

 API

Page 8: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-2 8/51www.bournemouth.ac.uk

MySQL APIs

Page 9: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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

Page 10: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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'%<erent MS an' the co'e t%$$ work.

• Vendor- specifc extensions: 

MS ,ec%Bc (MySQL %n th% cae"

Page 11: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-2 11/51www.bournemouth.ac.uk

MySQL PI

PHP ha C '%<erent PI to ue MySQL:• my$ 7 Stan'ar' MySQL 0ten%on

e,recate'3 # D#T )S03 %t w%$$ be remo+e'%n PHP 6

• my$% 7 MySQL %m,ro+e' eten%on• e,$ace tan'ar' my$ eten%on (my$"

• P# 7 PHP ata #bAect• my$ an' my$% ,roce'ura$ unct%on

$ook +ery %m%$ar e.*. mysql_query() 

mysqli_query() but they are '%<erent

Deprecated - Do Not Use ys!l extension" use ys!li orPD# instead$

Page 12: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-2 12/51www.bournemouth.ac.uk

My$% +. P#

Green = Advantae over other option

Page 13: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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,

Page 14: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-2 14/51

www.bournemouth.ac.uk

Usin) #ur MySQL Ser*er

Page 15: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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"

Page 16: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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)

Page 17: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-2 17/51

www.bournemouth.ac.uk

#racle Vs$ MySQL

Page 18: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-2 18/51

www.bournemouth.ac.uk

#rac$e +. MySQL

•  The ,ur,oe % not about he$,%n* youchooe the r%*ht 'atabae to ue a the

two are bu%$t or '%<erent market.• MySQL % ece$$ent or web 'e+e$o,ment

an' % a +ery ,o,u$ar an' ,oweru$ MSor th%

#rac$e % better u%te' to non7weba,,$%cat%on

• We are ore interested %n the'atabae tructure an' mechan%mrather than the%r eature et

!I like"understand

#racle$ can%t I

 &ust use that'(

Page 19: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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

Page 20: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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.

Page 21: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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 '%<erence % that 'ata ty,e

'eBne' %n MySQL are more specifc.9or eam,$e:• MySQL: IDT3 #)L03 0L an' 9L#T.

• #rac$e: D)M0(n3>"3 9L#T(!4".

Page 22: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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.

Page 23: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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 '%<erent. eware othe%r '%<erence.

• Many MS ue the%r own%m,$ementat%on o SQL 7 Dotab$y

#rac$e3 M%croot SQL Ser+er an'

Page 24: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-2 24/51

www.bournemouth.ac.uk

SQL Synta 0am,$e

• etr%e+e the Brt 5 row rom a'atabae tab$e

MySQLSELECT columns FROM table ORDER BY key ASC LIMIT 5;

#racleSELECT * FROM (SELECT columns FROMtable ORDER BY key ASC) WHERE ROWNUM 

<= 5;

Page 25: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-2 25/51

www.bournemouth.ac.uk

P,P and MySQL

Page 26: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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

Page 27: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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()

Page 28: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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'%<erent.

!"## = mysqli_!"##e!$(%&'&%+%i&',-5.%+

%/0ss1"r2%+%i&',-5.%);

mysqli_!"##e!$(host+ username+ password +

dbname+ port+ socket);

mysql 34 host 3u user 3/ password  db

Page 29: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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 +ould

need to use this is to chane databases a*ter connectin.

Page 30: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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$

Page 31: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-2 31/51

www.bournemouth.ac.uk

-) #,erat%on

• -) tan' or reate3 .ead3 Update an' Delete %n re$at%ona$ 'atabaea,,$%cat%on.

• It reer to INS/.01./A0/3 S/L/03UPDA0/ an' D/L/0/1D.#P SQL

tatement.

• -) o,erat%on are %m,$emente' a SQL!ueries1coands.

Page 32: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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.

Page 33: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-2 33/51

www.bournemouth.ac.uk

-): -reate Tab$e

• SQL ynta (ba%c":

• 0am,$e:

 

CREATE TABLE table (col type);

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$"!@ 9:T

)8;

resul$ = mysqli_query(!"##+ query);

,K%' ,Kname ,Ktock

IDT ;-H (!55" IDT

Page 34: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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$"!@) >AL6ES (:6LL+ %%+

);8;resul$ = mysqli_query(!"##+query);,K%' ,Kname ,Ktock

> >

Page 35: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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$"!@ = ;8;

resul$ = mysqli_query(!"##+query);

,K%' ,Kname ,Ktock

0 f 0

Page 36: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-2 36/51

www.bournemouth.ac.uk

-): ),'ate ata

• SQL ynta (ba%c":

• 0am,$e:

 

6DATE table SET col =%value%?ERE col = %value%;

query = 86DATE /r"2u!$s SET /_s$"!@ ='& ?ERE /_s$"!@ = ;8;

resul$ = mysqli_query(!"##+ query);

,K%' ,Kname ,Ktock

>     21

Page 37: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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$"!@ = ;8;

resul$ = mysqli_query(!"##+query);

Page 38: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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);

Page 39: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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 set where:• 0ach 'ata row w%$$ be tore' %n an array.

 The key o the array % the tab$e colun orco$umn index.

•  The +a$ue o the array % the data *alue corre,on'%n* to the co$umn.

• Loo,/%terat%on % ue' to *et a$$ 'ata row.

Page 40: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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

Page 41: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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$"!@ ;8;

resul$ = mysqli_query(!"##+

query);14ile (r"1 =mysqli_e$!4_r"1(resul$))

e!4" r"1+ r"1&+ r"1';

Page 42: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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$"!@FROM /r"2u!$s ?ERE /_s$"!@

;8;

resul$ = mysqli_query(!"##+ query);14ile (r"1 =mysqli_e$!4_0rr0y(resul$+ MYSHL9_:6M))

e!4" r"1+ r"1&;

Page 43: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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$"!@FROM /r"2u!$s ?ERE /_s$"!@

;8;

resul$ = mysqli_query(!"##+ query);14ile (r"1 =mysqli_e$!4_0rr0y(resul$+MYSHL9_ASSOC))

e!4" r"18/_i28+ r"18/_s$"!@8;

Page 44: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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$"!@FROM /r"2u!$s ?ERE /_s$"!@

;8;

resul$ = mysqli_query(!"##+ query);14ile (r"1 = mysqli_e$!4_0rr0y(resul$+

MYSHL9_BOT?))

e!4" r"18/_i28+ r"1&;

G

Page 45: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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, 

Page 46: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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

Page 47: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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</C"##e!$e2I<J/8;

!"## =mysqli_!"##e!$(8&'&8+8user#0me8+8/0ss1"r28+ 828) "r

2ie(mysqli_!"##e!$_err"r());

Page 48: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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</C"##e!$e2I<J/8;

Page 49: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-2 49/51

www.bournemouth.ac.uk

onclusion

Page 50: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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

Page 51: Web Units Lec 4 - PHP & MySQL v2(1)-2

7/23/2019 Web Units Lec 4 - PHP & MySQL v2(1)-2

http://slidepdf.com/reader/full/web-units-lec-4-php-mysql-v21-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