Top Banner
8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm) http://slidepdf.com/reader/full/oreilly-web-database-applications-with-php-and-mysql-chm 1/831 o n ly fo r R uB oard - do not distribute or recom p ile C opyright T a b le of C ontents Index F u ll D escription R e v ie w s R eader revie w s E rrata W eb D atab ase A pplications w ith P H P & M yS Q L H ugh E . W illia m s D avid Lane P u b lis h e r: O 'R e illy First E d itio n M arch 2002 IS B N : 0-596-00041-3, 582 pages W e b D a t a b a s e A p p l i c a t i o n s w i t h P H P a n d M y S Q L offers w eb developers a m ixture practical inform a tio n on creatin g w eb database a p p lic a tio n s . U sing P H P a technologies that are o fte n com b in e d to develop w eb a p p lic a tio n s , th e bo on designing relational databas es and on w eb a p p lic a tio n architecture, bo readers w ho have never d e a lt w ith these issues before. The book a ls o in O n lin e W in e S tore, a com plete (but fic t io n a l) o n lin e retail site im p le m ent o n ly fo r R uB oard - do not distribute or recom p ile h is docum ent w as created by an unregistered C hm M ag .
831

(Oreilly) Web Database Applications With Php and Mysql (Chm)

Apr 08, 2018

Download

Documents

Abel Saduwa
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    1/831

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    C o p y r i g h t T a b l e o f C o n t e n t s I n d e x F u l l D e s c r i p t i o n R e v i e w s R e a d e r r e v i e w s E r r a t a

    W e b D a t a b a s e A p p l i c a t i o n s w i t h P H P & M y S Q L

    H u g h E . W i l l i a m s D a v i d L a n e P u b l i s h e r : O ' R e i l l y

    F i r s t E d i t i o n M a r c h2 0 0 2 I S B N :0 - 5 9 6 - 0 0 0 4 1 - 3 , 5 8 2 p a g e s

    W e b D a t a b a s e A p p l i c a t i o n s w i t h P H P a n d M y S Q L o f f e r s w e b d e v e l o p e r s a m i x t u r ep r a c t i c a l i n f o r m a t i o n o n c r e a t i n g w e b d a t a b a s e a p p l i c a t i o n s . U s i n g P H P at e c h n o l o g i e s t h a t a r e o f t e n c o m b i n e d t o d e v e l o p w e b a p p l i c a t i o n s , t h e b oo n d e s i g n i n g r e l a t i o n a l d a t a b a s e s a n d o n w e b a p p l i c a t i o n a r c h i t e c t u r e , b or e a d e r s w h o h a v e n e v e r d e a l tw i t h t h e s e i s s u e s b e f o r e . T h e b o o k a l s o i nO n l i n e W i n e S t o r e , a c o m p l e t e ( b u t f i c t i o n a l ) o n l i n e r e t a i l s i t e i m p l e m e n t

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    h i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g.

    http://www.oreillynet.com/cs/catalog/view/au/763@x-t=book.viewhttp://www.oreillynet.com/cs/catalog/view/au/763@x-t=book.viewhttp://www.oreilly.com/catalog/webdbapps/errata/default.htmhttp://www.oreilly.com/cgi-bin/reviews@bookident=webdbappshttp://www.oreilly.com/catalog/webdbapps/reviews.htmlhttp://www.oreilly.com/catalog/webdbapps/desc.htmlhttp://www.oreillynet.com/cs/catalog/view/au/763@x-t=book.viewhttp://www.oreillynet.com/cs/catalog/view/au/762@x-t=book.viewhttp://www.oreilly.com/catalog/webdbapps/errata/default.htmhttp://www.oreilly.com/cgi-bin/reviews@bookident=webdbappshttp://www.oreilly.com/catalog/webdbapps/reviews.htmlhttp://www.oreilly.com/catalog/webdbapps/desc.html
  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    2/831

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    W e b D a t a b a s e A p p l i c a t i o n s w i t h P H P & M y S Q L

    P r e f a c e W h a t T h i s B o o k I s A b o u t W h a t Y o u N e e d t o K n o w H o w T h i s B o o k I s O r g a n i z e d H o w t o U s e T h i s B o o k C o n v e n t i o n s U s e d i n T h i s B o o k H o w t o C o n t a c t U s W e b S i t e a n d C o d e E x a m p l e s A c k n o w l e d g m e n t s

    1 . D a t a b a s e A p p l i c a t i o n s a n d t h e W e b 1 . 1 T h r e e - T i e r A r c h i t e c t u r e s 1 . 2 T h e C l i e n t T i e r 1 . 3 T h e M i d d l e T i e r 1 . 4 T h e D a t a b a s e T i e r 1 . 5 O u r C a s e S t u d y

    2 . P H P 2 . 1 I n t r o d u c i n g P H P 2 . 2 C o n d i t i o n s a n d B r a n c h e s 2 . 3 L o o p s 2 . 4 A W o r k i n g E x a m p l e 2 . 5 A r r a y s 2 . 6 S t r i n g s 2 . 7 R e g u l a r E x p r e s s i o n s 2 . 8 D a t e a n d T i m e F u n c t i o n s

    2 . 9 I n t e g e r a n d F l o a t F u n c t i o n s 2 . 1 0 U s e r - D e f i n e d F u n c t i o n s 2 . 1 1 O b j e c t s

    h i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    3/831

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    4/831

    9 . 4 W e b D a t a b a s e A p p l i c a t i o n s a n d A u t h e n t i c a t i o n 9 . 5 P r o t e c t i n g D a t a o n t h eW e b

    1 0 . W i n e s t o r e C u s t o m e r M a n a g e m e n t 1 0 . 1 O v e r v i e w o f t h e W i n e s t o r e A p p l i c a t i o n 1 0 . 2 C u s t o m e r M a n a g e m e n t 1 0 . 3 A u t h e n t i c a t i n g U s e r s 1 0 . 4 T h e W i n e s t o r e I n c l u d e F i l e s

    1 1 . T h e W i n e s t o r e S h o p p i n g C a r t 1 1 . 1 T h e W i n e s t o r e H o m e P a g e 1 1 . 2 T h e S h o p p i n g C a r t A r c h i t e c t u r e 1 1 . 3 M a n a g i n g R e d i r e c t i o n

    1 2 . O r d e r i n g a n d S h i p p i n g a t t h eW i n e s t o r e

    1 2 . 1 F i n a l i z i n g O r d e r s 1 2 . 2 H T M L a n d E m a i l R e c e i p t s

    1 3 . R e l a t e d T o p i c s 1 3 . 1 A u t o m a t e d H o u s e k e e p i n g 1 3 . 2 T e m p l a t e s 1 3 . 3 S e a r c h i n g a n d B r o w s i n g

    A . I n s t a l l a t i o n G u i d e

    A . 1 I n s t a l l i n g M y S Q L , A p a c h e , a n d P H P A . 2 I n s t a l l i n g t h e W i n e s t o r e E x a m p l e s A . 3 I n s t a l l i n g A p a c h e t o U s e S S L A . 4 I n s t a l l a t i o n R e s o u r c e s

    B . I n t e r n e t a n d W e b P r o t o c o l s B . 1 T h e I n t e r n e t B . 2 H y p e r t e x t T r a n s f e r P r o t o c o l

    C . M o d e l i n g a n d D e s i g n i n g R e l a t i o n a l D a t a b a s e s C . 1 T h e R e l a t i o n a l M o d e l C . 2 E n t i t y - R e l a t i o n s h i p M o d e l i n g

    D . M a n a g i n g S e s s i o n s i n t h e D a t a b a s e T i e r D . 1 U s i n g a D a t a b a s e t o K e e p S t a t e D . 2 P H P S e s s i o n M a n a g e m e n t D . 3 M y S Q L S e s s i o n S t o r e

    E . R e s o u r c e s

    E . 1 C l i e n t T i e r R e s o u r c e s E . 2 M i d d l e T i e r R e s o u r c e s E . 3 D a t a b a s e T i e r R e s o u r c e s

    h i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    5/831

    E . 4 S e c u r i t y a n d C r y p t o g r a p h y R e s o u r c e s

    C o l o p h o n

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    h i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a.

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    6/831

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    W e b D a t a b a s e A p p l i c a t i o n s w i t h P H P & M y S Q

    C o p y r i g h t 2 0 0 2 O ' R e i l l y & A s s o c i a t e s , I n c . A l l r i g h t s r e s

    P r i n t e d i n t h e U n i t e d S t a t e s o f A m e r i c a .

    P u b l i s h e d b y O ' R e i l l y & A s s o c i a t e s , I n c . , 1 0 0 5S e b a s t o p o l , C A 9 5 4 7 2 .

    O ' R e i l l y & A s s o c i a t e s b o o k s m a y b e p u r c h a s e do r s a l e s p r o m o t i o n a l u s e . O n l i n e e d i t i o n s a r e a l( h t t p : / / s a f a r i . o r e i l l y . c o m ) . F o r m o r e i n f o r m a t i o n c o n t a c t o u rc o r p o r a t e / i n s t i t u t i o n a l s a l e s d e p a r t m e n t : 8 0 0 - 9 9 8c o r p o r a t e @ o r e i l l y . c o m .

    T h e O ' R e i l l y l o g o i s a r e g i s t e r e d t r a d e m a r k o f Oi l l y & A s s o c i a t e s , I n c .M a n y o f t h e d e s i g n a t i o n s u s e d b y m a n u f a c t u r e r

    t h e i r p r o d u c t s a r e c l a i m e d a s t r a d e m a r k s . W h e ri n t h i s b o o k , a n d O ' R e i l l y & A s s o c i a t e s , I n c . wc l a i m , t h e d e s i g n a t i o n s h a v e b e e n p r i n t e d i n c aa s s o c i a t i o n b e t w e e n t h e i m a g e o f a p l a t y p u s a na p p l i c a t i o n s w i t h P H P a n d M y S Q L i s a t r a d e m aI n c .

    W h i l e e v e r y p r e c a u t i o n h a s b e e n t a k e n i n t h e p

    p u b l i s h e r a n d t h e a u t h o r a s s u m e n o r e s p o n s i b i lo r f o r d a m a g e s r e s u l t i n g f r o m t h e u s e o f t h e i n

    i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a s e g o t o h t

    mailto:[email protected]://../safari.oreilly.com/default.htmmailto:[email protected]://../safari.oreilly.com/default.htm
  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    7/831

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    h i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g.

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    8/831

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    P r e f a c e

    W e b d a t a b a s e a p p l i c a t i o n s i n t e g r a t e d a t a b a s e s a n dd e s t i n a t i o n s s u c h a s o n l i n e a u c t i o n s i t e s , r e t a i l s t o rp e r s o n a l i z e d h o m e p a g e s a r e a l l e x a m p l e s o f w e bt h e s e a p p l i c a t i o n s s t e m s f r o m t h e i r a c c e s s i b i l i t y a n da c c e s s t h e s a m e d a t a a t t h e s a m e t i m e w i t h o u t t h et h e i r m a c h i n e s .

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    h i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g.

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    9/831

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    W h a t T h i s B o o k I s A b o u t

    T h i s b o o k i s f o r d e v e l o p e r s w h o w a n t t o b u i l d d a t at h e W e b . I t p r e s e n t s t h e p r i n c i p l e s a n d t e c h n i q u e sw e b d a t a b a s e a p p l i c a t i o n s t h a t s t o r e , m a n a g e , a n dt e c h n i q u e s f o r s e c u r i n g a n a p p l i c a t i o n . T h e a r c h i t e cf r a m e w o r k f o r a p p l i c a t i o n s t h a t c a n r u n o n m o d e s tm i l l i o n h i t s p e r d a y f r o m u s e r s .

    A n i m p o r t a n t f e a t u r e o f t h i s b o o k i s o u r o n g o i n g c aH u g h a n d D a v e ' s O n l i n e

    W i n e s . I t ' s a c o m p l e t e b u t f i c t i o n a l o n l i n e r e t a i l s t o r es e a r c h a d a t a b a s e o f w i n e s , a d d i t e m s t o a s h o p p i np u r c h a s e w i n e s . S e a r c h i n g , b r o w s i n g , s t o r i n g u s e r du s e r t r a n s a c t i o n s , a n d s e c u r i t y a r e e a c h t h e s u b j e c ti l l u s t r a t e d w i t h e x a m p l e s f r o m t h e c a s e s t u d y . T h ep r e s e n t e d a n d b r i e f l y d i s c u s s e d a t t h e e n d o f t h e b

    W e u s e o p e n s o u r c e s o f t w a r e . O u r d a t a b a s e m a n a g

    s y s t e m k n o w n f o r i t s s u i t a b i l i t y t o a p p l i c a t i o n s t h a to v e r h e a d s . O u r s c r i p t i n g l a n g u a g e i s P H P , w h i c h i si n t e r a c t w i t h m o r e t h a n 1 5 r e l a t i o n a l d a t a b a s e s y s t eo t h e r s e r v i c e s . W e u s e P H P t o d e v e l o p t h e a p p l i c aa n d t h e r e l a t i o n a l d a t a b a s e m a n a g e m e n t s y s t e m ( Rc h o i c e .

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    h i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M.

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    10/831

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    W h a t Y o u N e e d t o K n o w

    T h i s b o o k i s a b o u t u n d e r s t a n d i n g a n d d e v e l o p i n g a p p l i c a t i o nl o g i c t h a t b r i n g s d a ta n d t h e W e b t o g e t h e r . W e i n t r o d u c e d a t a b a s e s y s t em s o v e r t h e c o u r s e o f t h e b o oo u r d i s c u s s i o n s d o n ' t r e p l a c e a b o o k o r c l a s s d e d i cb o o k a b o u t a s p e c i f i c r e l a t i o n a l d a t a b a s e s y s t e m s uy o u a r e a l r e a d y f a m i l i a r w i t h t h e W e b . W e i n t r o d u ck e y w e b p r o t o c o l s , H T M L , H T T P , a n d T C P / I P .

    W e a l s o a s s u m e y o u c a n p r o g r a m i n a t h i r d - g e n e r a

    C + + , J a v a , P e r l , F O R T R A N , o r V i s u a l B a s i c . O u r il a n g u a g e d o e s n ' t a s s u m e y o u a r e f a m i l i a r w i t h w e bb u t w e d o a s s u m e y o u u n d e r s t a n d t h e b a s i c H T M Lp o p u l a r w e b b r o w s e r s . I f y o u c a n a u t h o r a n H T M Lc o n t a i n s a< f o r m > a n d a< t a b l e > e l e m e n t , y o u h a v e s u f f i c i e n t H T MI t i s t h e p r i n c i p l e s o f s t r u c t u r e i n t h e m a r k u p p r o c ea t t r a c t i v e n e s s o r u s a b i l i t y o f t h e p r e s e n t a t i o n i n t h e

    H T M L c o n c e p t s a s r e q u i r e d , b u t a n H T M L g u i d e sH T M L a n d X H T

    T h e D e f i n i t i v e G u i d e , b y C h u c k M u s c i a n o a n d W i l l i a m K e n nu n d e r s t a n d i n g a n d b u i l d i n g w e b d a t a b a s e a p p l i c a t i o

    P r o g r a m m i n g P H P , b y R a s m u s L e r d o r f a n d K e v i n T a t r o e u

    Y o u d o n ' t n e e d a d e t a i l e d u n d e r s t a n d i n g o f r e l a t i o nw o r k i n g k n o w l e d g e i s h e l p f u l . W e p r e s e n t t h e r e l a td e v e l o p i n g s i m p l e a p p l i c a t i o n s , a n d w e c o v e r m a n yt e l l w h e n a d a t a b a s e i s t h e m e t h o d o f c h o i c e t o s t o

    d a t a b a s e q u e r y l a n g u a g e S Q L , a n d a c a s e s t u d y t hc o n v e r t s t h e m o d e l t o a d a t a b a s e d e s i g n . T h i s b o o kr e s o u r c e s o n d a t a b a s e t h e o r y , h o w e v e r , i t ' s e n o u g hd a t a b a s e s f o r m a n y w e b d a t a b a s e a p p l i c a t i o n s .

    W e b r i e f l y i n t r o d u c e w e b s e r v e r s a n d n e t w o r k i n g i nC h a p t e r 1 a n d p r o v i d e a d d i t i om a t e r i a l i nA p p e n d i x B . B o t h w e b s e r v e r s a n d n e t w o r k i n g a r ea p p l i c a t i o n b u t a r e n ' t t h e f o c u s o f t h i s b o o k . W e p r

    w e b s e r v e r a n d t o u n d e r s t a n d h o w i t f i t s i n t h e a r cF o r m a n y a p p l i c a t i o n s , t h i s i s s u f f i c i e n t . L i k e w i s e , ww i l l u n d e r s t a n d w h a t n e t w o r k i n g a n d n e t w o r k p r o t oe b d a t a b a s e

    i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a s e g o t o h t

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    11/831

    a p p l i c a t i o n d e s i g n .

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    h i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g.

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    12/831

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    H o w T h i s B o o k I s O r g a n i z e d

    T h e r e a r e 1 3 c h a p t e r s a n d 5 a p p e n d i x e s i n t h i s b oC h a p t e r 1 t oC h a p t e r 3 i n t r o d u c e w e b d a t a b a s e a p p l i c a t i o n s , P H P , M y S Q L , a n d S Q L :

    C h a p t e r 1

    D i s c u s s e s t h e t h r e e - t i e r a r c h i t e c t u r e c o m m o n l ya n d i n t h o s e t h a t w e d i s c u s s i n t h i s b o o k . W et h e f e a t u r e s o f e a c h , a n d w e i n t r o d u c e t h e s o f t wb r i e f l y i n t r o d u c e w e b p r o t o c o l s . T h e c h a p t e r c oc a s e s t u d y e x a m p l e , H u g h a n d D a v e ' s O n l i n e Wo ft h e w i n e s t o r e , t h e s y s t e m r e q u i r e m e n t s , a n d wd e v e l o p e a c h c o m p o n e n t a r e c o v e r e d .

    C h a p t e r 2

    I n t r o d u c e s t h e P H P s c r i p t i n g l a n g u a g e . I t c o v e rd i s c u s s e s t h e b a s i c p r o g r a m m i n g c o n s t r u c t s , v at e c h n i q u e s , a n d c o m m o n s o u r c e s o f b u g s . W e i nt o i l l u s t r a t e h o w t o p r o g r a m w i t h P H P .

    C h a p t e r 3

    I n t r o d u c e s t h e M y S Q L D B M S a n d h o w t o i n t e rl a n g u a g e S Q L . U s i n g e x a m p l e s f r o m t h e o n l i n ec o m m a n d s f o r c r e a t i n g , d e l e t i n g , a n d u p d a t i n gp r e s e n t a l o n g e r , e x a m p l e - d r i v e n s e c t i o n o n q u ec h a p t e r c o n c l u d e s w i t h d i s c u s s i o n o f a d v a n c e dt u n i n g a n d c o n f i g u r a t i o n .

    C h a p t e r 4 t oC h a p t e r 9 c o v e r t h e p r i n c i p l e s a n d p r a c t i c e o f d ea p p l i c a t i o n l o g i c .

    C h a p t e r 4

    I n t r o d u c e s t h e b a s i c s o f c o n n e c t i n g t o t h e M y Sq u e r y i n g p r o c e s s u s e d i n m o s t i n t e r a c t i o n s w i t hp r e s e n t e x a m p l e s

    i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a s e g o t o h t

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    13/831

    t h a t u s e m o s t o f t h e P H P M y S Q L l i b r a r y f u n c t id a t a b a s e q u e r i e s c a n b e f o r m a t t e d a s H T M L f oc h a p t e r i s s u p p o r t e d b y t h e o n l i n e w i n e s t o r e c ah o w t o b u i l d a m o d e r a t e l y c o m p l e x q u e r y i n g m

    C h a p t e r 5

    C o n t i n u e s t h e p r i n c i p l e s a n d p r a c t i c e o f q u e r y i n gu s e r - d r i v e n q u e r y i n g , i n w h i c h t h e u s e r p r o v i d ep r o c e s s . W e s h o w h o w d a t a i s e n c o d e d , s e n t i nw e b s e r v e r , a n d d e c o d e d f o r p r o c e s s i n g i n P H Pi m p l i c a t i o n s i n p r o c e s s i n g u s e r d a t a a n d s h o w s t e p s t o s e c u r e i ns y s t e m s . O u r d i s c u s s i o n i s s u p p o r t e d b y a u s e r

    r e s u l t s t h a t c a n b e b r o w s e d p a g e b y p a g e .

    C h a p t e r 6

    C o v e r s w r i t i n g d a t a t o w e b d a t a b a s e s . T h e r e a ri s d i f f e r e n t f r o m r e a d i n g i t . F o r e x a m p l e , r e l o a db r o w s e r c a n c a u s e d a t a t o b e w r i t t e n t o a d a t aa c c e s s i n g t h e s a m e d a t a b a s e i n t r o d u c e s o t h e r p

    u n e x p e c t e d l y b e i n g c h a n g e d b y o n e u s e r w h i l ed i s c u s s h o w t o s o l v e p r o b l e m s r e l a t e d t o t h e n aW e i l l u s t r a t e t h e p r i n c i p l e s w i t h a n e x a m p l e t h at h e o n l i n e w i n e s t o r e .

    C h a p t e r 7

    T h i s c h a p t e r i s r e l a t e d t oC h a p t e r 6 a n d p r e s e n t s t h e p r i n c i p l e s a

    u s e r - i n p u t v a l i d a t i o n . W e i n t r o d u c e v a l i d a t i o n mw o r k i n w e b d a t a b a s e a p p l i c a t i o n s a n d s h o w h oP H P a n d s u p p o r t e d b y c l i e n t - s i d e , b r o w s e r - b a s e

    C h a p t e r 8

    C o v e r s t h e p r i n c i p l e s o f a d d i n g s e s s i o n m a n a g e mS e s s i o n m a n a g e m e n t a l l o w s t h e i n t e r a c t i o n s b e t

    b e r e l a t e d s o t h a t , f o r e x a m p l e , a u s e r c a n l o gb e g u i d e d t h r o u g h a s e r i e s o f s t e p s i n a p r o c e ss e s s i o n s a n d i l l u s t r a t e t h e t e c h n i q u e s w i t h a c a

    i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a s e g o t o h t

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    14/831

    f e e d b a c k t o u s e r s w h o a r e j o i n i n g a s c u s t o m e r s

    C h a p t e r 9

    P r e s e n t s t o p i c s i n w e bs e c u r i t y . W e s h o w h o w P H P c a n b e u s e d

    a u t h e n t i c a t i o n , h o w d a t a b a s e s c a n m a n a g e m a nc a n b e s e c u r e d w i t h t h e n e t w o r k - l e v e l s e c u r e sl o g i n a n d l o g o u t p r o c e s s f o r t h e o n l i n e w i n e s t o rs e s s i o n m a n a g e m e n t i nC h a p t e r 8 .

    C h a p t e r 1 0 t oC h a p t e r 1 3 p r e s e n t a n d o u t l i n e t h e c o m p l e t e d w i no u t l i n e s a r e n ' t c o m p r e h e n s i v e : w e a s s u m e y o u h a v e c o m p l e t e dC h a p t e r 4 t oC h a p t e r 9 a n d u n d e r s t a n d t h e p r i n c i p l e s o f d e v e l o p i n g w e b d a

    t h a t y o u v i e w , e d i t , a n d u s e t h e w i n e s t o r e P H P s c rC h a p t e r 1 0 t h r o u g hC h a p t e r 1 3 .

    C h a p t e r 1 0

    P r e s e n t s t h e c o d e f o r c u s t o m e r m a n a g e m e n t i ng e n e r a l - p u r p o s e f u n c t i o n s t h a t a r e u s e d t h r o u g hp r e s e n t e d i s b a s e d o n t h e e x a m p l e s d e v e l o p e dC h a p t e r 4 t oC h a p t e r 8 .

    W e p r e s e n t t h e s c r i p t s f o r c o l l e c t i n g , v a l i d a t i n g ,a l s o i n c l u d e t h e c o d e f o r t h e u s e r l o g i n a n d l o gp r e s e n t e d i nC h a p t e r 9 .

    C h a p t e r 1 1

    P r e s e n t s t h e c o d e f o r t h e s h o p p i n g c a r t a t t h es h o p p i n gc a r t i s s t o r e d i n a d a t a b a s e , a n d e a c h u s e r ' s c a r t i s t r a

    f r o mC h a p t e r 8 . T h e c a r t m o d u l e a l l o w s a u s e r t o v i e we r c a r t , a d d i t e m s t o t h e c a r t , u p d a t e i t e mq u a n t i t i e s , d e l e t e i t e m s , a n d e m p t y t h e

    C h a p t e r 1 2

    P r e s e n t s t h e c o d e f o r t h e o r d e r i n g a n d s h i p p i n go r d e r i n g p r o c e s s s h o w s h o w t h e c o m p l e x d a t a bd i s c u s s e d i nC h a p t e r 3 a n dC h a p t e r 6 a r e u s e d t o c o n v e r t a s h o p pc u s t o m e r o r d e r . W e a l s o s h o w h o w e m a i l c o n f iu s e r , a n d a n o r d e r c o n f i r m a t i o n i s p r e s e n t e d a s

    i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a s e g o t o h t

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    15/831

    C h a p t e r 1 3

    C o n c l u d e s t h e c a s e s t u d y e x a m p l e s a n d p r e s e np r e s e n t t h e c o m p l e t e s e a r c h i n g a n d b r o w s i n g wt e c h n i q u e s d i s c u s s e d i nC h a p t e r 5 . W e a l s o d i s c u s s a u t o m a t i n g

    t e m p l a t e s t o s e p a r a t e s c r i p t c o d e f r o m H T M L m

    T h e r e a r e f i v e a p p e n d i x e s i n t h i s b o o k :

    A p p e n d i x A

    A c o n c i s e g u i d e t o i n s t a l l i n g t h e A p a c h e w e b sL i n u x o p e r a t i n g s y s t e m ; i n c l u d e s r e s o u r c e p o i n t

    g u i d e s f o r L i n u x a n d o t h e r o p e r a t i n g s y s t e m s .

    A p p e n d i x B

    B u i l d s o nC h a p t e r 1 a n d d e s c r i b e s t h e w o r k i n g s o f t h e W

    A p p e n d i x C

    C o n t a i n s a c a s e s t u d y t h a t m o d e l s t h e s y s t e m r e qe n t i t y - r e l a t i o n s h i p d a t a b a s e m o d e l i n g . I t s h o w sa d e s i g n .I t a l s o d e t a i l s t h e S Q L c o m m a n d s u s e d t o c r e a t

    A p p e n d i x D

    A n e x t e n s i o n o fC h a p t e r 8 , t h i s a p p e n d i x s h o w s h o w t h e d e fs e s s i o n h a n d l i n g c a n b e m o v e d t o t h e m o r e s c a

    A p p e n d i x E

    L i s t s u s e f u l r e s o u r c e s , i n c l u d i n g w e b s i t e s a n do n t h e t o p i c s p r e s e n t e d t h r o u g h o u t t h i s b o o k .

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    h i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g.

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    16/831

    o n l y f o r R u B o a r d - d o n o td i s t r i b u t e o r r e c o m p i l e

    H o w t o U s e T h i s B o o k

    T h i s b o o k i s d e s i g n e d a s a t u t o r i a l - s t y l e i n t r o d u c t i o n t o w e b d a t a bp p l i c a t i o n s .

    I f y o u h a v e n ' t i n s t a l l e d t h e A p a c h e w e b s e r v e r , t h ed a t a b a s e m a n a g e m e n t s y s t e m , b e g i n w i t hA p p e n d i x A . A p p e n d i x A l i s t s p o s s i b l e m ef o r o b t a i n i n g t h e s o f t w a r e a n d i n c l u d e s i n s t r u c t i o n ss o u r c e c o d e .A p p e n d i x A a l s o s h o w s h o w t h e e x a m p l e s u s e d i nd o w n l o a d e d a n d i n s t a l l e d l o c a l l y . W e r e c o m m e n d oa t a b a s e s u s e d i n t h i s b o o k , a s t h e y w i l l h e l p y o u u n d e r s t a n d t h e c o n c e p t s a s t h

    d a t a b a s e c o n f i g u r a t i o n s t e p s a r e i n c l u d e d a t t h e b eC h a p t e r 3 .

    E a c h c h a p t e r c o v e r s a d i f f e r e n t t o p i c .C h a p t e r 1 t h r o u g hC h a p t e r 3 c a n b er e a d i n d e p e n d e n t l y .C h a p t e r 1 i n t r o d u c e s w e b d a t a b a s e a p p l i c a t i o n sa p p l i c a t i o n . W e r e c o m m e n d r e a d i n gC h a p t e r 1 f i r s t .C h a p t e r 2 a n d C h a p t e r 3 a r e d e s i g n e da s i n t r o d u c t i o n s t o P H P a n d S Q L , r e s p e c t i v e l y ; b o tb e u s e d a s r e f e r e n c e s w h e n r e a d i n g t h e l a t e r c h a p t e r s .

    C h a p t e r 4 t h r o u g hC h a p t e r 9 a r e a m a j o r s e c t i o n w i t h a t u t o r i a l st h e p r i n c i p l e s a n d p r a c t i c e o f w e b d a t a b a s e a p p l i c aC h a p t e r 4 , C h a p t e r 5 , a n dC h a p t e r 6 b e g i n w i t h b a s i c p r i n c i p l e s a n d c o m p o n e n t s .C h a p t e r 7 , C h a p t e r 8 , a n dC h a p t e r9 c o n t a i n m o r e s o p h i s t i c a t e d e x a m p l e s t h a t r e l y o n c o n c e p t s f r oT h e s e c h a p t e r s a r e d e s i g n e d t o b e r e a d s e q u e n t i a l l y . B y t h e c o n c l u s i o n o fC h a p t e r 9 , y o us h o u l d h a v e m a s t e r e d t h e p r i n c i p l e s o f d e v e l o p i n g

    C h a p t e r 1 0 t oC h a p t e r 1 3 p r e s e n t a n d b r i e f l y d i s c u s s t h e c o m p lt h e o n l i n e w i n e s t o r e c a s e s t u d y . T h e s c r i p t s s h o w hC h a p t e r 4 t o C h a p t e r 9 a r e a p p l i e d i np r a c t i c e a n d , a s s u c h , a r e m o s t u s e f u lo f t h e e a r l i e r c h a p t e r s . T h e m a t e r i a l i n t h e s e l a t e r ct h e e x a m p l e a p p l i c a t i o n h a s b e e n d o w n l o a d e d a n d i n s t a l l e d o n a l o c a l s e r v e r , a l l o w i ns c r i p t s t o b e m o d i f i e d a n d t e s t e d a s t h e c h a p t e r s a

    A p p e n d i x B a n dA p p e n d i x C a r e a l s o i n a t u t o r i a l s t y l e . W e r e c o mA p p e n d i x B i f y o ua r e i n t e r e s t e d i n o r a r e u n f a m i l i a r w i t h t h e w e b e n vA p p e n d i x C i s a b r i e f i n t r o d u c t i o n t o e n t i t y - r e l a t i o n s h i p mt h e s t e p s w e t o o k i n d e s i g n i n g t h e w i n e s t o r e d a t a bA p p e n d i xC a f t e r c o m p l e t i n gC h a p t e r 3 , a n d o n l y i f a d e t a i l e d u n d e r s t a n d i n ge w i n e s t o r e

    i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a s e g o t o h t

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    17/831

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    18/831

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    19/831

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    H o w t o C o n t a c t U s

    P l e a s e a d d r e s s c o m m e n t s a n d q u e s t i o n s c o n c e r n i n g

    O ' R e i l l y & A s s o c i a t e s , I n c . 1 0 0 5 G r a v e n s t e i n H i g h w a y N o r t h S e b a s t o p o l , C A 9 5 4 7 2 ( 8 0 0 ) 9 9 8 - 9 9 3 8 ( i n t h e U n i t e d S t a t e s o r C a n a d a ) ( 7 0 7 ) 8 2 9 - 0 5 1 5 ( i n t e r n a t i o n a l o r l o c a l ) ( 7 0 7 ) 8 2 9 - 0 1 0 4 ( f a x )

    T h e r e i s a w e b p a g e f o r t h i s b o o k , w h i c h l i s t s e r r ai n f o r m a t i o n . Y o u c a n a c c e s s t h i s p a g e a t :

    h t t p : / / w w w . o r e i l l y . c o m / c a t a l o g / w e b d b a p p s /

    T o c o m m e n t o r a s k t e c h n i c a l q u e s t i o n s a b o u t t h i s b

    b o o k q u e s t i o n s @ o r e i l l y . c o m

    F o r m o r e i n f o r m a t i o n a b o u t b o o k s , c o n f e r e n c e s , R eN e t w o r k , s e e t h e O ' R e i l l y w e b s i t e a t :

    h t t p : / / w w w . o r e i l l y . c o m

    T h e a u t h o r s c a n b e r e a c h e d a t :

    h u g h @ c o m p u t e r . o r g d a v e @ s i m d b . c o m

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    h i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g.

    mailto:[email protected]:[email protected]://www.oreilly.com/default.htmhttp://www.oreilly.com/catalog/webdbapps/default.htmmailto:[email protected]:[email protected]://www.oreilly.com/default.htmmailto:[email protected]://www.oreilly.com/catalog/webdbapps/default.htm
  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    20/831

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    W e b S i t e a n d C o d e E x a m p l e s

    C o d e e x a m p l e s f r o m t h i s b o o k , d a t a u s e d t o c r e a t ec o m p l e t e d w i n e s t o r e a p p l i c a t i o n c a n b e f o u n d a t t hh t t p : / / w w w . o r e i l l y . c o m / c a t a l o g / w e b d b a p p s / o r a t t h e a u t h o r s ' w e b s i t e , h t t p : / / w w w . w e b d a t a b a s e b o o k . c o m .

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    h i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g.

    http://www.webdatabasebook.com/default.htmhttp://www.oreilly.com/catalog/webdbapps/default.htm
  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    21/831

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    22/831

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    C h a p t e r 1 . D a t a b a s e A p p l i c a t i o

    W i t h t h e g r o w t h o f t h e W e b o v e r t h e p a s t d e c a d e ,s e r v i c e s t h a t a r e a c c e s s i b l e o v e r t h e W e b . M a n y n ed r i v e n f r o m d a t a s t o r e d i n d a t a b a s e s . E x a m p l e s o fn e w s s e r v i c e s t h a t p r o v i d e a c c e s s t o l a r g e d a t a r e ps u c h a s o n l i n e s t o r e s , a n d b u s i n e s s - t o - b u s i n e s s ( B 2

    D a t a b a s e a p p l i c a t i o n s h a v e b e e n a r o u n d f o r o v e r 3u s i n g n e t w o r k t e c h n o l o g y l o n g b e f o r e t h e W e b e x i s

    b y b a n k t e l l e r s a r e o b v i o u s e x a m p l e s o f e a r l y n e t wa r e i n s t a l l e d i n b a n k b r a n c h e s , a n d a c c e s s t o t h e bp r o v i d e d t h r o u g h a w i d e a r e a n e t w o r k . T h e s e e a r l yo r g a n i z a t i o n s t h a t c o u l d a f f o r d t h e s p e c i a l i z e d t e r mb u i l d a n d o w n t h e n e t w o r k i n f r a s t r u c t u r e .

    T h e W e b p r o v i d e s c h e a p , u b i q u i t o u s n e t w o r k i n g . I ts t a n d a r d i z e d w e b b r o w s e r s o f t w a r e t h a t r u n s o n a

    d e v e l o p e r s , w e b s e r v e r s o f t w a r e i s f r e e l y a v a i l a b l ed o c u m e n t s a n d p r o g r a m s . S e v e r a l s c r i p t i n g l a n g u a gd e v e l o p p r o g r a m s t o u s e w i t h w e b s e r v e r s a n d w e b

    T h i s b o o k i s a b o u t b r i n g i n g t o g e t h e r t h e W e b a n da p p l i c a t i o n s d o t h i s t h r o u g h t h r e e l a y e r s o f a p p l i c a tm a n a g e m e n t s y s t e m ( D B M S ) a n d a d a t a b a s e . A t t ha n i n t e r f a c e t o t h e a p p l i c a t i o n . B e t w e e n t h e t w o l i e

    d e v e l o p e d w i t h a w e b s e r v e r - s i d e s c r i p t i n g l a n g u a gc a n d e c o d e a n d p r o d u c e H T M L u s e d f o r p r e s e n t a t i

    W e b e g i n b y d i s c u s s i n g t h e t h r e e - t i e r a r c h i t e c t u r e ma p p l i c a t i o n s . W e t h e n i n t r o d u c e t h e n a t u r e o f t h e Wt h e n d i s c u s s e a c h o f t h e t h r e e t i e r s a n d t h e i r c o m pO n l i n e W i n e s , o u r c a s e s t u d y a p p l i c a t i o n , i s i n t r o d ut o i t f r e q u e n t l y t h r o u g h o u t t h e c o u r s e o f t h e b o o k a

    c o n s t r u c t i o n o f a w e b d a t a b a s e a p p l i c a t i o n .

    i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a s e g o t o h t

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    23/831

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    h i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g.

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    24/831

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    25/831

    W h e n w e u s e t h e t e r m " t h e W e b , " w e m e a n t h r e eb a s e d o n t h e s e s t a n d a r d s : t h e H y p e r t e x t M a r k u pT r a n s f e r P r o t o c o l ( H T T P ) , a n d t h e T C P / I P n e t w o r k is t r u c t u r i n g a n d p r e s e n t i n g i n f o r m a t i o n u s i n g a w e be f f e c t i v e n e t w o r k i n g p r o t o c o l t h a t t r a n s f e r s d a t a b e th a s l i t t l e i m p a c t o n w e b d a t a b a s e a p p l i c a t i o n d e v e ld a t a b a s e a p p l i c a t i o n s i s i n t e r f a c i n g t r a d i t i o n a l d a t a bH T T P . T h i s i s w h e r e t h e c o m p l e x a p p l i c a t i o n l o g i c

    1 . 1 . 1 H y p e r t e x t T r a n s f e r P r o t o c o l

    T h e t h r e e - t i e r a r c h i t e c t u r e p r o v i d e s a c o n c e p t u a l f

    a p p l i c a t i o n s . T h e W e b i t s e l f p r o v i d e s t h e p r o t o c o l sm i d d l e t i e r s o f t h e a p p l i c a t i o n ; t h a t i s , i t p r o v i d e s ta n d t h e w e b s e r v e r . H T T P i s o n e c o m p o n e n t t h a t bA d e t a i l e d k n o w l e d g e o f H T T P i s n ' t n e c e s s a r y t o ui t ' s i m p o r t a n t t o u n d e r s t a n d t h e p r o b l e m s H T T P p r eT h e H T T Pp r o t o c o l i s u s e d b y w e b b r o w s e r s t o r e q u e s tf o r w e b s e r v e r s t o r e t u r n r e s p o n s e s . ( A l o n g e r i n t r op r o t o c o l s i n c l u d i n g m o r e e x a m p l e s o f H T T P r e q u e

    A p p e n d i x B . )

    H T T P a l l o w s r e s o u r c e s t o b e c o m m u n i c a t e d a n d s h

    p e r s p e c t i v e , H T T P i s a na p p l i c a t i o n s - l a y e r p r o t o c o l t h a t i s b u i l t o n t o p o fn e t w o r k i n g p r o t o c o l s u i t e . M o s t w e b s e r v e r s a n d wc u r r e n t v e r s i o n , H T T P / 1 . 1 . S o m e b r o w s e r s a n d s e r vH T T P / 1 . 0 , b u t m o s t H T T P / 1 . 1 s o f t w a r e i s b a c k w a r d

    H T T P c o m m u n i c a t i o n s d o m i n a t e I n t e r n e t n e t w o r k t ra b o u t 7 5 % o f a l l t r a f f i c . [ 1 ] W e s p e c u l a t e t h a t t h i s p e r c e n t a g e i s ng r o w t h i n t h e n u m b e r a n d p o p u l a r i t y o f H T T P - b a s es e r v i c e s .

    [ 1 ] F r o m K . T h o m p s o n , G . J . M i l l e r , a n d R . W i l d e r . " W i d e - a r e a i n tc h a r a c t e r i s t i c s , "I E E E N e t w o r k , 1 1 ( 6 ) : 1 0 - 2 3 , N o v e m b e r / D e c e m b e r 1 9 9 7 .

    1 . 1 . 1 . 1 H T T P e x a m p l e

    H T T P i s c o n c e p t u a l l y s i m p l e : a c l i e n t w e b b r o w s e rr e q u e s t f o r a r e s o u r c e t o

    i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a s e g o t o h t

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    26/831

    w e b s e r v e r , a n d t h e w e b s e r v e r s e n d s b a c k ar e s p o n s e . T h e H T T P r e s p o n s er e s o u r c e t h e H T M L d o c u m e n t , i m a g e , o r o u t p u t oa s i t s p a y l o a d . T h i s s i m p l e r e q u e s t - r e s p o n s e m o d e lF i g u r e 1 - 2 .

    F i g u r e 1 - 2 . A w e b b r o w s e r m a k e s a r e q u e s t a n d t h e w

    A n H T T P r e q u e s t i s a t e x t u a l d e s c r i p t i o n o f a r e s oC o n s i d e r t h e f o l l o w i n g e x a m p l e r e q u e s t :

    G E T / i n d e x . h t m l H T T P / 1 . 0 F r o m : h u g h @ c o m p u t e r . o r g ( H u g h W i l l i a m s ) U s e r - a g e n t : H u g h - f a k e - b r o w s e r / v e r s i o n - 1 . 0 A c c e p t : t e x t / p l a i n , t e x t / h t m l

    T h i s e x a m p l e u s e s aG E T m e t h o d t o r e q u e s t a n H T M L p a g ei n d e x . h t m l w i t h H T T P / 1 . 0t h i s e x a m p l e , t h r e e a d d i t i o n a l h e a d e r l i n e s i d e n t i f yd e f i n e w h a t d a t a t y p e s c a n b e a c c e p t e d b y t h e b r ow e b b r o w s e r a n d m a y i n c l u d e o t h e r h e a d e r s ; t h e pb y t y p i n g t h e r e q u e s t i n t o T e l n e t s o f t w a r e .

    A n H T T P r e s p o n s e h a s a r e s p o n s e c o d e a n d m e s st h e r e s o u r c e t h a t h a s b e e n r e q u e s t e d . A n e x a m p l ei n d e x . h t m l

    i s a s f o l l o w s :

    H T T P / 1 . 0 2 0 0 O K D a t e : S a t , 2 1 J u l 2 0 0 2 0 3 : 4 4 : 2 5 G M T S e r v e r : A p a c h e / 1 . 3 . 2 0 C o n t e n t - t y p e : t e x t / h t m l C o n t e n t - l e n g t h : 8 8

    L a s t - m o d i f i e d : F r i , 1 F e b 2 0 0 2 0 3 : 4 0 : 0 3 G M T

    < h t m l > < h e a d >

    i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a s e g o t o h t

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    27/831

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    28/831

    1 . 1 . 2 T h i n C l i e n t s

    G i v e n t h a t a w e b d a t a b a s e a p p l i c a t i o n b u i l t w i t h an a t u r a l l y w i t h H T T P , w h y u s e t h a t m o d e l a t a l l ? T h

    t h et h i n c l i e n t . W e b b r o w s e r s a r e v e r y t h i n c l i e n t s : l i t t l e ac l i e n t t i e r . T h e b r o w s e r s i m p l y s e n d s H T T P r e q u e s tr e s p o n s e s , w h i c h c o n t a i n m o s t l y H T M L d o c u m e n t s .

    A t h r e e - t i e r m o d e l m e a n s y o u d o n ' t h a v e t o b u i l d ,u s e r w h o h a s a w e b b r o w s e r c a n u s e t h e w e b d a t an e e d i n g t o i n s t a l l a d d i t i o n a l s o f t w a r e , b e u s i n g a s pp a r t i c u l a r h a r d w a r e p l a t f o r m . T h i s m e a n s a n a p p l i c ad i v e r s e , g e o g r a p h i c a l l y d i s p e r s e d u s e r s . T h e a d v a n tt h i s b o o k i s e n t i r e l y o n t h r e e - t i e r s o l u t i o n s w i t h t h i s

    B u t w h a t a r e t h e a l t e r n a t i v e s t o a t h i n c l i e n t ? A c ut h i c k e r c l i e n t t h a t c a n s t i l l f i t t h e t h r e e - t i e r m o d e l : tm o r e o f t h e o v e r a l l a p p l i c a t i o n l o g i c o n h e r p l a t f o r mt i e r t h a t , i n t u r n , p r o v i d e s a n i n t e r f a c e t o t h e d a t a br a t h e r t h a n u s i n g t h e g e n e r i c b r o w s e r s o l u t i o n , a c up r o b l e m s i n h e r e n t i n t h e s t a t e l e s s n e s s , s e c u r i t y , a n dm i g h t n o t e v e n u s e H T T P t o c o m m u n i c a t e w i t h t h e

    A t h i c k c l i e n t i s a l s o p a r t o f a t r a d i t i o n a l t w o - t i e r sc l i e n t / s e r v e r

    a r c h i t e c t u r e . M o s t t r a d i t i o n a l d a t a b a s e a p p l i c a t i o n s s u c ho n l y t w o t i e r s . T h e c l i e n t t i e r h a s m o s t o f t h e o v e rt h e D B M S i t s e l f . T h e a d v a n t a g e i s t h a t a c u s t o m i z ee x a c t a p p l i c a t i o n r e q u i r e m e n t s w i t h o u t a n y c o m p r o m

    h a r d w a r e a n d o p e r a t i n g s y s t e m f l e x i b i l i t y a n d t h e r eu s e r .

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    h i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g.

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    29/831

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    1 . 2 T h e C l i e n t T i e r

    T h e c l i e n t t i e r i n t h e t h r e e - t i e r a r c h i t e c t u r e m o d e l is o f t w a r e p r o c e s s e s a n d d i s p l a y s H T M L r e s o u r c e s , ia n d p r o c e s s e s H T T P r e s p o n s e s . A s d i s c u s s e d e a r l i eu s i n g a w e b b r o w s e r a s t h e t h i n - c l i e n t l a y e r , i n c l u dw i d e r a n g e o f p l a t f o r m s .

    T h e r e a r e m a n y b r o w s e r p r o d u c t s a v a i l a b l e , a n d e af e a t u r e s . T h e t w o m o s t p o p u l a r w i n d o w i n g - b a s e d b

    E x p l o r e r . W h i l e w e w o n ' t d e s c r i b e a l l t h e f e a t u r e s ob a s i c s e t :

    A l l w e b b r o w s e r s a r e H T T P c l i e n t s t h a t s e n d r ew e b s e r v e r s ( u s u a l l y i n a g r a p h i c a l e n v i r o n m e n

    A l l b r o w s e r s i n t e r p r e t p a g e s m a r k e d u p w i t h H Tt h e y p r e s e n t t h e h e a d i n g s , i m a g e s , h y p e r t e x t l i n

    S o m e b r o w s e r s d i s p l a y i m a g e s , p l a y m o v i e s a no b j e c t s .

    M a n y b r o w s e r s c a n r u n J a v a S c r i p t t h a t i s e m b eu s e d , f o r e x a m p l e , t o v a l i d a t e a< f o r m > o r c h a n g e h o w a p a g e i s po n u s e r a c t i o n s .

    S e l e c t e d w e b b r o w s e r s c a n r u n c o m p o n e n t s d e vp r o g r a m m i n g l a n g u a g e s . T h e s e c o m p o n e n t s o f t et o o l s t h a t c a n ' t b e i m p l e m e n t e d i n H T M L , o r o t

    S e v e r a l b r o w s e r s c a n a p p l y C a s c a d i n g S t y l e S hc o n t r o l t h e p r e s e n t a t i o n o f H T M L e l e m e n t s .

    T h e r e a r e s u b t l e a n d s o m e t i m e s n o t s o s u b t l e d id i f f e r e n t b r o w s e r s h a v e i n r e n d e r i n g a n H T M L p a g

    b r o w s e r a n d d o e s n ' t d i s p l a y i m a g e s o r r u n J a v a S c rt h e t e x t o n a p a g e a s s o u n d t h e s p o k e n w o r d p rv i s i o n - i m p a i r e d . M a n y s u b t l e b u t a n n o y i n g d i f f e r e n cr t f o r C S S a n d t h e

    i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a s e g o t o h t

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    30/831

    f e a t u r e s o f t h e l a t e s t H T M L s t a n d a r d , H T M L 4 .

    W e b b r o w s e r s a r e t h e m o s t o b v i o u s e x a m p l e o f au s e r a g e n t , a s o f t w a r e c l i e n t t h

    r e q u e s t s r e s o u r c e s f r o m a w e b s e r v e r . O t h e r u s e r aw e b

    s p i d e r s a u t o m a t e d s o f t w a r e t h a t c r a w l s t h e W e b a n dp r o x y

    c a c h e s , s o f t w a r e s y s t e m s t h a t r e t r i e v e a n d l o c a l l y s t o ro t h e r u s e r a g e n t s .

    W h i l e t h i s b o o k i s n ' t a g u i d e t o w r i t i n g H T M L , w et h r o u g h o u t t h e b o o k . P o i n t e r s t o r e s o u r c e s t h a t d e sa n d t h e d i r e c t i o n o f w e b p a g e s t a n d a r d s a r e i n c l u dA p p e n d i x E . W e i n t r o d u c eJ a v a S c r i p t c l i e n t - s i d e s c r i p t i n g f o r v a l i d a t i o n o f d a t ab r o w s e r i nC h a p t e r 7 .

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    h i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g.

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    31/831

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    1 . 3 T h e M i d d l e T i e r

    I n m o s t t h r e e - t i e r w e b d a t a b a s e s y s t e m s , t h e m a j o rm i d d l e t i e r . T h e c l i e n t t i e r p r e s e n t s d a t a t o a n d c o lt i e r s t o r e s a n d r e t r i e v e s t h e d a t a . T h e m i d d l e t i e r s e r v e s m o s t o f t h e r e m ab r i n g t o g e t h e r t h e o t h e r t i e r s : i t d r i v e s t h e s t r u c t u r eu s e r , a n d i t p r o c e s s e s i n p u t f r o m t h e u s e r a s i t i s fr e a d o r w r i t e d a t a . I t a l s o a d d s s t a t e m a n a g e m e n ta p p l i c a t i o n l o g i c i n t e g r a t e s t h e W e b w i t h t h e d a t a b a

    I n t h e a p p l i c a t i o n f r a m e w o r k u s e d i n t h i s b o o k , t h ew e b s e r v e r , a w e b s c r i p t i n g l a n g u a g e , a n d t h e s c r i p t i n g l a n g u a g e e n

    p r o c e s s e s H T T P r e q u e s t s a n d f o r m u l a t e s r e s p o n s e sa p p l i c a t i o n s , t h e s e r e q u e s t s a r e o f t e n f o r p r o g r a m sd a t a b a s e m a n a g e m e n t s y s t e m . T h e w e b s e r v e r w e

    A p a c h e S o f t w a r e F o u n d a t i o n ' s A p a c h e H T T P s e r v e r , t h e o p e n s o u r c e w e bb y m o r e t h a n 6 0 % o f I n t e r n e t c o n n e c t e d c o m p u t e r s[ 2 ]

    [ 2 ] F r o mT h e N e t c r a f t W e b S e r v e r S u r v e y , h t t p : / / w w w . n e t c r a f t . c o m / s u r v e y / ( A p r i l 2 0 0 1 ) .

    W e u s e t h eP H P s c r i p t i n g l a n g u a g e a s o u r m i d d l e - t i e r s c r i p t i n g l a n go p e n s o u r c e p r o j e c t o f t h e A p a c h e S o f t w a r e F o u n dp o p u l a r A p a c h e H T T P s e r v e r a d d - o n m o d u l e , w i t hs e r v e r s h a v i n g P H P c a p a b i l i t i e s . [ 3 ] P H P i s p a r t i c u l a r l y s u i t e d t o w e bb e c a u s e o f i t s i n t e g r a t i o n t o o l s f o r t h e W e b a n d d af l e x i b i l i t y o f e m b e d d i n g s c r i p t s i n H T M L p a g e s p e r

    T h e d a t a b a s e - t i e r i n t e g r a t i o n s u p p o r t i s a l s o e x c e l l et o i n t e r a c t w i t h a l m o s t a l l p o p u l a r d a t a b a s e m a n a g e

    [ 3 ] F r o m t h e S e c u r i t y S p a c e w e b s e r v e r s u r v e y , A p a c h e m o d u l e r eh t t p : / / w w w . s e c u r i t y s p a c e . c o m / s _ s u r v e y / d a t a / i n d e x . h t m l ( A p r i l 2 0 0 1 ) .

    1 . 3 . 1 W e b S e r v e r s

    W e b s e r v e r s a r e o f t e n r e f e r r e d t o a sH T T P s e r v e r s . T h e t e r m " H T T P s e r v es u m m a r y o f t h e i r f u n c t i o n : t h e i r b a s i c t a s k i s t o l i s tr e c e i v e H T T P r e q u e s t s m a d e b y u s e r a g e n t s ( u s u a le r v e t h e r e q u e s t s ,

    i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a s e g o t o h t

    http://www.securityspace.com/s_survey/data/index.htmlhttp://www.netcraft.com/survey/default.htmhttp://www.securityspace.com/s_survey/data/index.htmlhttp://www.netcraft.com/survey/default.htm
  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    32/831

    a n d r e t u r n H T T P r e s p o n s e s t h a t c o n t a i n t h e r e q u e s

    T h e r e a r e e s s e n t i a l l y t w o t y p e s o f r e q u e s t m a d e t of i l e o f t e n a s t a t i c H T M L w e b p a g e o r a n i m a g e a p r o g r a m t o b e r u n a n d i t s o u t p u t t o b e r e t u r n e d

    f i l e s a r e f u r t h e r d i s c u s s e d i nA p p e n d i x B .

    R e q u e s t s f o r w e b s c r i p t s t h a t a c c e s s a d a t a b a s e a rr e q u i r e a s e r v e r t o r u n a p r o g r a m . W i t h t h e s o f t w a

    a r e f o r P H P s c r i p t r e s o u r c e s , w h i c h r e q u i r e t h a t t h eP H P Z e n d e n g i n e b e r u n , a s c r i pr e t r i e v e d a n d p r o c e s s e d , a n d t h e s c r i p t o u t p u t c a p t u

    1 . 3 . 1 . 1 T h e A p a c h e H T T P s e r v e r , V e r s i o n 1 . 3

    L i k e m o s t u s e r s o f t h e A p a c h e H T T P s e r v e r , w e cA p a c h e . A p a c h e i s a n o p e nw e b s e r v e r . T h e c u r r e n t r e l e a s e a t t h e t i m e o f w r i t i

    T h e i n s t a l l a t i o n a n d c o n f i g u r a t i o n o f A p a c h e f o r m os t r a i g h t f o r w a r d . A c o n c i s e i n s t a l l a t i o n g u i d e f o r t h e L i n u x o p e r a t i n gA p p e n d i x A . A p a c h e c a n b e d o w n l o a d e d f r o mh t t p : / / w w w . a p a c h e . o r g ; o t h e r A p a c h er e s o u r c e s a r e l i s t e d i nA p p e n d i x E .

    A p a c h e i s f a s t a n d s c a l a b l e . I t c a n h a n d l e s i m u l t a nd e s i g n e d t o r u n u n d e r m u l t i t a s k i n g o p e r a t i n g s y s t e mM i c r o s o f t W i n d o w s . I t ' s a l s o l i g h t w e i g h t , h a s l o w ph a n d l e c h a n g e s i n r e q u e s t l o a d s , a n d c a n r u n f a s t

    A p a c h e a t l e a s t c o n c e p t u a l l y i s n ' t c o m p l i c a t e d . Tp r o c e s s e s , w h e r e o n e p r o c e s s c o o r d i n a t e s t h e o t h e r

    r u n s w i t h t h e p e r m i s s i o n s o f t h es u p e r u s e r o r r o o t u s e r o n a U n i x m a c hs e r v e r e q u e s t s i t s e l f . T h e o t h e r p r o c e s s e s , w h i c h u sp e r m i s s i o n l e s s u s e r s , n o t i f y t h e i r a v a i l a b i l i t y t o h a nI f t o o f e w s e r v e r s a r e a v a i l a b l e t o h a n d l e i n c o m i n gs t a r t n e w s e r v e r s ; i f t o o m a n y a r e f r e e , i t m a y k i l l

    H o w A p a c h e l i s t e n s o n t h e n e t w o r k a n d s e r v e s r e qf i l e . T h e s e r v e r a d m i n i s t r a t o r c o n t r o l s t h e b e h a v i o rd i r e c t i v e s t h a t a f f e c t r e s o u r c e r e q u i r e m e n t s , r e s p o n sr e q u e s t l o a d v a r i a b i l i t y , s e c u r i t y , h o w H T T P r e q u e s

    i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a s e g o t o h t

    http://www.apache.org/default.htmhttp://www.apache.org/default.htm
  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    33/831

    o t h e r a s p e c t s o f i t s o p e r a t i o n . C a r e f u l a d j u s t m e n t op e r f o r m a n c e , a n d m o r e d e t a i l s o f A p a c h e c o n f i g u r al i s t e d i nA p p e n d i x E .

    1 . 3 . 1 . 2 T h e A p a c h e H T T P s e r v e r , V e r s i o n 2 . 0

    V e r s i o n 1 . 3 o f A p a c h e h a s s o m e l i m i t a t i o n s t h a t w i2 . 0 i s a v a i l a b l e f o r d o w n l o a d , b u t a t t h e t i m e o f w rO n l y a r o u n d 2 0 s i t e s a r e k n o w n t o b e u s i n g t h e b e

    T h e s i g n i f i c a n t e n h a n c e m e n t s i n A p a c h e 2 . 0 a r e :

    U s e o f l i g h t e r - w e i g h t p r o c e s s e s o rt h r e a d s i n c o n j u n c t i o n w i t h t h e po n t h e o l d e r v e r s i o n s . T h i s w i l l m o s t l i k e l y o f f ei m p r o v e m e n t i n s t a r t i n g n e w s e r v e r s a n d r e d u c eo f r u n n i n g s e r v e r s .

    B e t t e r s u p p o r t , p e r f o r m a n c e , a n d s t a b i l i t y o n n o

    A d d i t i o n o f f i l t e r i n g m o d u l e s s o t h a t d a t a c a n bw e b s e r v e r .

    S u p p o r t f o r I P v 6 , t h e n e w v e r s i o n o f t h e I P p r o

    1 . 3 . 2 W e b S c r i p t i n g w i t h P H P

    P H P h a s e m e r g e d a s a c o m p o n e n t o f m a n y m e d i ua p p l i c a t i o n s . T h i s i s n ' t t o s a y t h a t o t h e r s c r i p t i nf e a t u r e s . H o w e v e r , t h e r e a r e m a n y r e a s o n s t h a t m a

    P H P i s o p e n s o u r c e , m e a n i n g i t i s e n t i r e l y f r e em a i n t a i n a n d i m p r o v e i t a r e u n c o n s t r a i n e d b y c

    O n e o r m o r e P H P s c r i p t s c a n b e e m b e d d e d i n tc l i e n t - t i e r i n t e g r a t i o n e a s y . O n t h e d o w n s i d e , t hp r e s e n t a t i o n ; h o w e v e r t h e t e m p l a t e t e c h n i q u e s dC h a p t e r 1 3 c a n s o l v em o s t o f t h e s e p r o b l e m s .

    T h e r e a r e o v e r 1 5 l i b r a r i e s f o r n a t i v e , f a s t a c c e

    i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a s e g o t o h t

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    34/831

    F a s t e x e c u t i o n o f s c r i p t s . W i t h t h e n e w i n n o v a tp r o c e s s i n g , e x e c u t i o n i s f a s t , a n d a l l c o m p o n e ns p a c e o f P H P ( i n c o n t r a s t t o o t h e r s c r i p t i n g f r ai n d i s t i n c t m o d u l e s ) . E m p i r i c a l e v i d e n c e s u g g e sc o m p l e x i t y , P H P i s f a s t e r t h a n o t h e r p o p u l a r s c

    P l a t f o r m a n d o p e r a t i n g - s y s t e m f l e x i b i l i t y . A p a c ha n d u n d e r s e l e c t e d o p e r a t i n g s y s t e m s ; P H P r u ni n t e g r a t e d w i t h o t h e r w e b s e r v e r s .

    P H P i s s u i t e d t o c o m p l e x s y s t e m s d e v e l o p m e n tl a n g u a g e , w i t h m o r e t h a n 5 0 f u n c t i o n l i b r a r i e s .

    T h e c u r r e n t v e r s i o n o f P H P i s V e r s i o n 4 w e c a l lb o o k a n d t h e c u r r e n t r e l e a s e a t t h e t i m e o f w r i t i n g

    P H P 4 r e p r e s e n t s a c o m p l e t e r e w r i t e o f t h e u n d e r l y is i g n i f i c a n t d i f f e r e n c e i s a c h a n g e i n t h e m o d e l u s e d

    T h e P H P 3 s c r i p t i n g e n g i n e w a s a ni n t e r p r e t e r . E a c h l i n e o f c o d e i n a s c rp a r s e d , a n d e x e c u t e d . I f a s t a t e m e n t i n t h e b o d y oc o d e i s r e i n t e r p r e t e d 1 0 0 t i m e s u s i n g P H P 3 . T h i s m

    f a s t f o r s h o r t s c r i p t s .

    T h e P H P 4 s c r i p t - p r o c e s s i n g m o d e l i s d i f f e r e n t a n ds c r i p t i s r e a d , p a r s e d , a n d c o m p i l e d i n t o a n i n t e r m e

    c o d e i s e x e c u t e d b y t h e P H P 4 Z e n d e n g i n e s c r i p te x e c u t o r . T h i s m e a n s t h a t e a ct h e s c r i p t i s i n t e r p r e t e d f r o m i t s r a w f o r m o n l y o n ct i m e s . M o r e o v e r , c o m p i l a t i o n a l l o w s o p t i m i z a t i o n o fp e r f o r m a n c e i m p r o v e m e n t i n P H P 4 f o r a l l b u t v e r y

    T h e a r c h i t e c t u r e o f t h e P H P 4 s c r i p t i n g e n v i r o n m e n tF i g u r e 1 - 3 ( i m a g e f r o mZ e n d T e c h n o l o g i e s I n c . ) . A s s h o w n , P H P 4 i s a m oP H P s o f t w a r e i t s e l f i s d i v i d e d i n t o t w o c o m p o n e n t st h e Z e n d e n g i n e .

    F i g u r e 1 - 3 . T h e a r c h i t e c t u r e o f t h e P H P 4 s c r i p t

    i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a s e g o t o

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    35/831

    W h e n a u s e r a g e n t m a k e s a r e q u e s t t o t h e w e b s e

    T h e w e b s e r v e r p a s s e s t h e r e q u e s t t o t h e Z e n d1 .

    T h e w e b s e r v e r i n t e r f a c e c a l l s t h e Z e n d e n g i n ee n g i n e .

    2 .

    T h e P H P s c r i p t i s r e t r i e v e d f r o m d i s k b y t h e e n3 .

    T h e s c r i p t i s c o m p i l e d b y t h e r u n t i m e c o m p i l e r .4 .

    T h e c o m p i l e d c o d e i s r u n b y t h e e n g i n e ' s e x e c um o d u l e s . T h e o u t p u t o f t h e e x e c u t o r i s r e t u r n e d

    5 .

    T h e w e b s e r v e r i n t e r f a c e r e t u r n s o u t p u t t o t h e wt h e o u t p u t a s a n H T T P r e s p o n s e t o t h e u s e r a g

    6 .

    H o w t h e P H P s c r i p t i n g e n g i n e i s m a n a g e d a n d r u ni n c l u d e d i n t h e A p a c h e w e b s e r v e r i n s t a l l a t i o n p r o c

    A p p e n d i x A , t h e P H P m o d u l e l i b r a r y i ss t a t i c a l l y l i n k e d w i t h t h e A p a c h eh t t p d b i n a r y

    e x e c u t a b l e . T h i s m e a n s t h a t t h e P H P s c r i p t i n g e n g iA p a c h e r u n s , m a k i n g t h e P H P e n g i n e r u n f a s t e r . T

    s t a t i c P H P l i b r a r y c o n s u m e s m o r e m e m o r y t h a n i fd y n a m i c a l l y , a n dt h a t t h e m o d u l e u p g r a d e p r o c e s s i s l e s s f l e x i b l e .

    P o i n t e r s t o w e b r e s o u r c e s , b o o k s , a n d c o m m e r c i a ll i s t e d i nA p p e n d i x E .

    h i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    36/831

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    h i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g.

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    37/831

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    1 . 4 T h e D a t a b a s e T i e r

    T h e d a t a b a s e t i e r i s t h e b a s e o f a w e b d a t a b a s e ar e q u i r e m e n t s , c h o o s i n g d a t a b a s e - t i e r s o f t w a r e , d e s i ga r e t h e f i r s t s t e p s i n s u c c e s s f u l w e b d a t a b a s e a p p l it e c h n i q u e s f o r m o d e l i n g s y s t e m r e q u i r e m e n t s , c o n vp r i n c i p l e s o f d a t a b a s e t e c h n o l o g y i nA p p e n d i x C . I n t h i s s e c t i o n , w e f o c u sc o m p o n e n t s o f t h e d a t a b a s e t i e r a n d i n t r o d u c e d a t ao t h e r t e c h n i q u e s f o r s t o r i n g d a t a .C h a p t e r 3 c o v e r s t h e s t a n d a r d s a n d s om o r e d e t a i l .

    I n a t h r e e - t i e r a r c h i t e c t u r e a p p l i c a t i o n , t h e d a t a b a s em a n a g e m e n t t y p i c a l l y i n c l u d e s s t o r a g e a n d r e t r i e v a l

    a l l o w i n g s i m u l t a n e o u s , o rc o n c u r r e n t , a c c e s s b y m o r e t h a n o n e m i d dp r o v i d i n g s e c u r i t y , e n s u r i n g t h e i n t e g r i t y o f d a t a , a nd a t a b a c k u p . I n m a n y w e b d a t a b a s e a p p l i c a t i o n s , t h

    R D B M S s y s t e m , a n d t h e d a t a s t o r e d i n ar e l a t i o n a l d a t a b a s e .

    M a n a g i n g r e l a t i o n a l d a t a i n t h e t h i r d t i e r r e q u i r e s cm o s t D B M S s a r e d e s i g n e d s o t h a t t h e s o f t w a r e c o ma D B M S , s k i l l s a r e r e q u i r e d t o d e s i g n a d a t a b a s e a

    t h e D B M S . F o r m o s t D B M S s , t h e q u e r y l a n g u a g e oS Q L . A n u n d e r s t a n d i nt h e u n d e r l y i n g a r c h i t e c t u r e o f t h e D B M S i s u n i m p o

    I n t h i s b o o k , w e u s e t h eM y S Q L R D B M S t o m a n a g e d a t a . M u c h l im i d d l e - t i e r s c r i p t i n g l a n g u a g e , t h e r e a r e o f t e n a r g u m e n t s a b o u t w h i c h D B

    t o a n a p p l i c a t i o n . M y S Q L h a s a w e l l - d e s e r v e d r e p u t a t i o n f o r s p e e d , a n dw e l l d e s i g n e d f o r a p p l i c a t i o n s w h e r e r e t r i e v a l o fd a t a i s m o r e c o m m o n t h a nw h e r e s m a l l , s i m p l e u p d a t e s a r e t h eg e n e r a l c l a s s o f m o d i f i c a t i o n s . T h e s ec h a r a c t e r i s t i c s t y p i c a l o f m o s t w e b d a t a b a s e a p p l i c aM y S Q L i s o p e n s o u r c e s o f t w a r e . H o w e v e r , t h e r e a rl a t e r i n t h i s s e c t i o n .

    T h e r e a r e o t h e r , n o n r e l a t i o n a l D B M S s o f t w a r e c h o i

    T h e s e i n c l u d e s e a r c h e n g i n e s , d o c u m e n t m a n a g e m es e r v i c e s s u c h a s e m a i l s o f t w a r e . O u r d i s c u s s i o n s i nt e c h n o l o g y i n t h e d a t a b a s e t i e r .

    i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a s e g o t o h t

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    38/831

    1 . 4 . 1 D a t a b a s e M a n a g e m e n t S y s t e m s

    A d a t a b a s e m a n a g e m e n t s y s t e m s t o r e s , s e a r c h e s , a

    A d a t a b a s e i s a c o l l e c t i o n o f r e l a t e d d a t a . T h e d a t a s t o rr o w s , t h a t m a k e u p a s i m p l e a d d r e s s b o o k o f n a m e s , a d dc o n t r a s t , t h e d a t a b a s e c a n a l s o c o n t a i n m i l l i o n s o fp u r c h a s e s , o r d e r s , a n d p a y r o l l o f a l a r g e c o m p a n y .H u g h a n d D a v e ' s O n l i n e W i n e s , i s a n e x a m p l e o f ab e t w e e n t h e s e t w o e x t r e m e s .

    A D B M S i s a s e t o f c o m p o n e n t s f o r d e f i n i n g , c o n sW h e n w e r e f e r t o a d a t a b a s e m a n a g e m e n t s y s t e m ,o r R D B M S . R e l a t i o n a l d a t a b a s e s s t o r e a n d m a n a g ee x a m p l e , c u s t o m e r s p l a c i n g o r d e r s , c u s t o m e r o r d e r sb e i n g p a r t o f a w i n e - g r o w i n g r e g i o n .

    F i g u r e 1 - 4 s h o w s t h e s i m p l i f i e d a r c h i t e c t u r e o f a t y p i c a l

    F i g u r e 1 - 4 . T h e a r c h i t e c t u r e o f a t y p i c a l D

    A D B M S c o n s i s t s o f s e v e r a l c o m p o n e n t s :

    h i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    39/831

    A p p l i c a t i o n s i n t e r f a c e

    L i b r a r i e s f o r c o m m u n i c a t i n g w i t h t h e D B M S . M o s ti n t e r p r e t e r t h a t o f t e n u s e s t h e s e l i b r a r i e s t o r e l a y r e

    D B M S a n d t o d i s p l a y r e s p o n s e s . I n a w e b d a t a b a si n t e r p r e t e r i s u s u a l l y r e p l a c e d b y a f u n c t i o n l i b r a r yl a n g u a g e .

    S Q L i n t e r p r e t e r

    A p a r s e r t h a t c h e c k s t h e s y n t a x o f i n c o m i n g q u e r yi n t e r n a l r e p r e s e n t a t i o n .

    Q u e r y e v a l u a t o r

    G e n e r a t e s d i f f e r e n t p l a n s f o r e v a l u a t i n g a q u e r y b y

    p r o p e r t i e s , s e l e c t s o n e o f t h e s e p l a n s , a n d t r a n s l a t ea r e e x e c u t e d .

    D a t a a c c e s s

    T h e m o d u l e s t h a t m a n a g e a c c e s s t o t h e d a t a s t o r e d

    m a n a g e r , a r e c o v e r y m a n a g e r , t h e m a i n - m e m o r y b ua n d t h e f i l e a n d a c c e s s m e t h o d m a n a g e r .

    D a t a b a s e

    T h e p h y s i c a l d a t a i t s e l f s t o r e d i n d a t a f i l e s . T h e d ai n d e x f i l e s f o r f a s t

    a c c e s s t o d a t a , a n d d a t a b a s e a n d s y s t e m s u m m a r yg e n e r a t i o n a n d o p t i m i z a t i o n .

    i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a s e g o t o h t

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    40/831

    T h e i m p o r t a n t c o m p o n e n t s f o r w e b d a t a b a s e a p p l i ca p p l i c a t i o n s i n t e r f a c e . F o r a l l b u t l a r g e - s c a l e a p p l i c at h e o t h e r c o m p o n e n t s o f a D B M S i s u s u a l l y u n n e c e

    1 . 4 . 2 W h y U s e a D B M S ?

    A q u e s t i o n t h a t i s o f t e n a s k e d i s : w h y u s e a c o m ps e v e r a l r e a s o n s t h a t c a n b e e x p l a i n e d b y c o n t r a s t i ns i m p l e t e x t f i l e , o r a c u s t o m - b u i l t m e t h o d o f s t o r i n gD B M S s h o u l d a n d s h o u l d n o t b e u s e d a r e d i s c u s s e

    T a k e s p r e a d s h e e t s a s a n e x a m p l e . S p r e a d s h e e t w os p e c i f i c a p p l i c a t i o n . I f t w o u s e r s s t o r e n a m e s a n d ad a t a i n a d i f f e r e n t w a y d e p e n d i n g o n t h e i r n e e d s a r o u n d a n d s u m m a r i z e t h e d a t a . I n t h i s s c h e m e , t hi n d e p e n d e n t : m o v i n g a c o l u m n m i g h t m e a n r e w r i t i nd a t a b e t w e e n t h e t w o u s e r s ' a p p l i c a t i o n s m i g h t b ed a t a b a s e p r o v i d e d a t a - p r o g r a m i n d e p e n d e n c e , w h e ro r d e r o f t h e s t o r e d i n f o r m a t i o n , a n d h o w t h e d a t a it h e s o f t w a r e t h a t a c c e s s e s i t .

    M a n a g i n g c o m p l e x r e l a t i o n s h i p s i s d i f f i c u l t i n a s p rc o n s i d e r o u r o n l i n e w i n e s t o r e : i f w e w a n t t o s t o r e ia l l o c a t e a f e w s p r e a d s h e e t c o l u m n s t o s t o r e e a c h cw e r e t o a d d b u s i n e s s a d d r e s s e s a n d p o s t a l a d d r e s sc o m p l e x p r o c e s s i n g t o , f o r e x a m p l e , p r o c e s s a m a ii n f o r m a t i o n a b o u t t h e p u r c h a s e s b y o u r c u s t o m e r s ,a n d p r o b l e m s s t a r t t o e m e r g e . F o r e x a m p l e , i t i s d i

    n u m b e r o f c o l u m n s n e e d e d t o s t o r e o r d e r s a n d t o dr e p o r t i n g .

    S p r e a d s h e e t s o r t e x t f i l e s d o n ' t w o r k w e l l w h e n t h eb e t w e e n s t o r e d d a t a i t e m s . I n c o n t r a s t , D B M S s a r e

    r e l a t i o n a l d a t a . D B M S s a r e a l s o a c o m p l e t e s o l u t i o n : i fd e s i g n a c u s t o m s p r e a d s h e e t o r f i l e s o l u t i o n . T h e mo f t e n t h e q u e r y l a n g u a g e S Q L a r e i n d e p e n d e n t o f

    a c t u a l l y p r o c e s s e d .

    A D B M S u s u a l l y p e r m i t s m u l t i u s e r t r a n s a c t i o n s . M e

    i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a s e g o t o h t

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    41/831

    f e a t u r e s t h a t c o n t r o l t h e w r i t i n g o f d a t a b y m u l t i p l es p r e a d s h e e t s h o u l d b e o p e n e d a n d w r i t t e n o n l y b ys p r e a d s h e e t , s h e w o n ' t s e e a n y u p d a t e s b e i n g m a db e s t , a s h a r e d s p r e a d s h e e t o r t e x t f i l e p e r m i t s v e r y

    A n a d d i t i o n a l b e n e f i t o f a D B M S i s i t s s p e e d . I t i s np r o v i d e s f a s t e r s e a r c h i n g o f d a t a t h a n a s p r e a d s h e ec a s e s , s e a r c h i n g a s p r e a d s h e e t o r a s p e c i a l - p u r p o s ee v e n f a s t e r i f i t i s d e s i g n e d c a r e f u l l y a n d t h e v o l u mm a n a g i n g l a r g e a m o u n t s o f r e l a t e d i n f o r m a t i o n , t h eD B M S c a n p e r m i t f a s t s e a r c h i n g , a n d i f i n f o r m a t i o no p t i m i z e t h e m e t h o d o f r e t r i e v i n g t h e d a t a .

    T h e r e a r e a l s o o t h e r a d v a n t a g e s o f D B M S s , i n c l u d is e c u r i t y , a d m i n i s t r a t i o n s o f t w a r e , a n d d a t a r e c o v e r ya p p l i c a t i o n d e v e l o p m e n t t i m e : t h e s y s t e m i s a l r e a d ya c c e s s t h e d a t a .

    1 . 4 . 2 . 1 E x a m p l e s o f w h e n t o u s e a D B M S

    I n a n y o f t h e s e s i t u a t i o n s , a D B M S s h o u l d p r o b a b l

    T h e r e i s m o r e t h a n o n e u s e r w h o n e e d s t o a c c

    T h e r e i s a t l e a s t a m o d e r a t e a m o u n t o f d a t a . Fm a i n t a i n i n f o r m a t i o n a b o u t a f e w h u n d r e d c u s t o

    T h e r e a r e r e l a t i o n s h i p s b e t w e e n t h e s t o r e d d a t am a y h a v e a n y n u m b e r o f r e l a t e d p u r c h a s e o r d e

    T h e r e i s m o r e t h a n o n e k i n d o f d a t a r e c o r d . F oi n f o r m a t i o n a b o u t c u s t o m e r s , o r d e r s , i n v e n t o r y ,

    T h e r e a r e c o n s t r a i n t s t h a t m u s t b e r i g i d l y e n f o rl e n g t h s , f i e l d t y p e s , u n i q u e n e s s o f c u s t o m e r n u m

    N e w o r c o n s o l i d a t e d i n f o r m a t i o n m u s t b e p r o d u

    t h a t i s , t h e d a t a m u s t b e q u e r i e d t o p r o d u c e r e p

    T h e r e i s a l a r g e a m o u n t o f d a t a t h a t m u s t b e s

    i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a s e g o t o h t

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    42/831

    S e c u r i t y i s i m p o r t a n t . T h e r e i s a n e e d t o e n f o r cd a t a .

    A d d i n g , d e l e t i n g , o r m o d i f y i n g d a t a i s a c o m p l e

    1 . 4 . 2 . 2 E x a m p l e s o f w h e n n o t t o u s e a D B M S

    T h e r e a r e s o m e s i t u a t i o n s w h e r e a r e l a t i o n a l D B M SH e r e a r e s o m e e x a m p l e s :

    T h e r e i s o n e t y p e o f d a t a i t e m , a n d t h e d a t a i se n t r y i s w r i t t e n w h e n a u s e r l o g s i n a n d l o g s oa s i m p l e t e x t f i l e m a y b e s u f f i c i e n t .

    T h e d a t a - m a n a g e m e n t t a s k i s t r i v i a l . I n t h i s c a sw e b s c r i p t i n t h e m i d d l e t i e r , r a t h e r t h a n a d d i n ge a c h t i m e t h e d a t a i s n e e d e d .

    T h e d a t a r e q u i r e s c o m p l e x a n a l y s i s . F o r a n a l y ss t a t i s t i c a l s o f t w a r e m a y b e m o r e a p p r o p r i a t e .

    1 . 4 . 3 T h e M y S Q L D B M S

    M y S Q L i s a m e d i u m - s c a l e D B M S , w i t h m o s t o f t ht h e a b i l i t y t o m a n a g e v e r y l a r g e q u a n t i t i e s o f d a t a .t h e d a t a b a s e s t h a t a r e t y p i c a l o f m a n y w e b d a t a b a s

    T h e d i f f e r e n c e b e t w e e n M y S Q L a n d s o m e o t h e r s yq u e r y i n g s u p p o r t a n d h a s l i m i t e d c o n c u r r e n c y - h a n d lt e n s o f m i d d l e - t i e r p r o c e s s e s c a n a c c e s s a d a t a b a s e

    T w o q u e r y i n g t e c h n i q u e s s p e c i f i c a l l yn e s t e d q u e r y i n g a n dv i e w s a r e n ' t s u p p o r t eb u t s u p p o r t i s p l a n n e d f o r t h e n e a r f u t u r e i n M y S Ql i m i t a t i o n s t h a t d o n ' t t y p i c a l l y a f f e c t w e b d e v e l o p m e

    T h e l i m i t a t i o n s o f M y S Q L u s u a l l y h a v e a v e r y m i nd e v e l o p m e n t . H o w e v e r , f o r h i g h - t h r o u g h p u t s y s t e m so r a p p l i c a t i o n s t h a t m o d i f y t h e d a t a b a s e f r e q u e n t l y ,s e c o n d c h o i c e w o u l d b e P o s t g r e S Q L , w h i c h i s k n oc o n c u r r e n t u s e r s . M o r e i n f o r m a t i o n o n P o s t g r e S Q L

    i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a s e g o t o h t

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    43/831

    h t t p : / / w w w . p o s t g r e s q l . o r g .

    A t t h e t i m e o f w r i t i n g , t h e c u r r e n t v e r s i o n o f M y S Q3 . 2 3 . 3 8 . M y S Q L r e s o u r c e s a r e l i s t e d i nA p p e n d i x E .

    1 . 4 . 4 S Q L

    S Q L i s t h e s t a n d a r d r e l a t i o n a l d a t a b a s e i n t e r a c t i o nd a t a b a s e s y s t e m s , i n c l u d i n g M y S Q L , s u p p o r t S Q La n d q u e r y d a t a b a s e s . I n d e e d , t h i s i s a n i m p o r t a n t p j u s t a q u e r y l a n g u a g e ; i t i s a f u l l y f l e d g e d t o o l f o r

    1 . 4 . 4 . 1 H i s t o r y

    S Q L h a s h a d a c o m p l i c a t e d l i f e . I t b e g a n a t t h e I B

    e a r l y 1 9 7 0 s , w h e r e i t w a s k n o w n a sS e q u e l ; s o m e u s e r s s t i l l c a l l i t S e q um o r e c o r r e c t l y r e f e r r e d t o b y t h e t h r e e - l e t t e r a c r o n yd e v e l o p m e n t a n d d i f f e r i n g i m p l e m e n t a t i o n s , t h e s t a np u b l i s h e d a n S Q L s t a n d a r d i n 1 9 8 6 . I B M p u b l i s h e d

    S i n c e t h e m i d - 1 9 8 0 s , t h r e e s u b s e q u e n t s t a n d a r d s hT h e f i r s t , S Q L - 8 9 , i s t h e m o s t w i d e l y , c o m p l e t e l y i ms y s t e m s . M a n y s y s t e m s i m p l e m e n t o n l y s o m e f e a t uS Q L - 9 2 , a n d a l m o s t n o s y s t e m s h a v e i m p l e m e n t e da p p r o v e d s t a n d a r d , S Q L - 9 9 o r S Q L - 3 .

    W e f o c u s o n f e a t u r e s f o u n d i n t h e M y S Q L D B M S .s t a n d a r d .

    1 . 4 . 4 . 2 S Q L c o m p o n e n t s

    S Q L h a s f o u r m a j o r p a r t s , a n d w e d i s c u s s t w o o f( D D L ) a n d t h e D a t a M a n i p u l a t i o n L a n g u a g e ( D M L )C h a p t e r 3 . T h e f o u r m a jc o m p o n e n t s o f S Q L a r e :

    D a t a D e f i n i t i o n L a n g u a g e

    i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a s e g o t o

    http://www.postgresql.org/default.htmhttp://www.postgresql.org/default.htm
  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    44/831

    D D L i s t h e s e t o f S Q L c o m m a n d s t h a t c r e a t e a n dt a b l e s , c r e a t e i n d e x e s , a n d m o d i f y e a c h o f t h e s e . Dg e n e r a l l y u s e d o n l y d u r i n g t h e c o n s t r u c t i o n o f t h e d a t a b a s e . I n d e x e s a r e

    o f d a t a .

    D a t a M a n i p u l a t i o n L a n g u a g e

    D M L i s t h e s e t o f c o m m a n d s t h a t w o r k w i t h a D B Mi n c l u d e t h o s e t o s e a r c h , i n s e r t , a n d d e l e t e d a t a . T h e s e c o m m

    i n t e r a c t w i t h a d a t a b a s e d u r i n g i t s n o r m a l u s e .

    T r a n s a c t i o n m a n a g e m e n t

    S Q L i n c l u d e s c o m m a n d s f o r t r e a t i n g a s e t o f c o m mt r a n s a c t i o n . U s i n g

    t h e s e t o o l s , t r a n s a c t i o n s c a n b e u n d o n e , o r r o l l e d b a c k .

    A d v a n c e d f e a t u r e s

    D M L a n d D D L i n c l u d e a d v a n c e d f e a t u r e s f o r e m b ep r o g r a m m i n g l a n g u a g e s ( i n m u c h t h e s a m e w a y y oi n P H P i nC h a p t e r 4 ) a n d d e f i n i n g s p e c i a l - p u r p o s e v i e w s o f t hg r a n t i n g a n d r e m o v i n g a c c e s s r i g h t s t o t h e D B M S

    c o m m a n d s f o r e n s u r i n g t h e i n t e g r i t y o f t h e s y s t e m ;t h a t r e l a t i o n a l c o n s t r a i n t s a r e m a i n t a i n e d c o r r e c t l y .

    T r a n s a c t i o n m a n a g e m e n t a n d a d v a n c e d f e a t u r e s o fC h a p t e r3 a n dC h a p t e r 6 , a n d i nA p p e n d i x C . P o i n t e r s t o r e f e r e n c e s o n S Q LA p p e n d i x E .

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    h i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g.

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    45/831

    o n l y f o r R u B o a r d - d o n o t d i s t r i b u t e o r r e c o m p i l e

    1 . 5 O u r C a s e S t u d y

    T h e p r i n c i p l e s o f w e b d a t a b a s e a p p l i c a t i o n s a r e i lb o o k w i t h t h e r u n n i n g e x a m p l e o f H u g h a n d D a v e '

    w i n e s t o r e t h r o u g h o u t t h e b o o k .

    T h e w i n e s t o r ea p p l i c a t i o n h a s m a n y c o m p o n e n t s o f a t y p il i c a t i o n , i n c l u d i n g :

    W e b p a g e s p o p u l a t e d w i t h d a t a f r o m a d a t a b a s

    U s e r - d r i v e n q u e r y i n g a n d b r o w s i n g , i n w h i c h t hl i m i t t h e s e a r c h i n g o r b r o w s i n g o f t h e d a t a b a s e

    D a t a e n t r y a n d v a l i d a t i o n . H T M L< f o r m > w i d g e t s c o l l e c t d a t a , a n d Jc l i e n t - s i d e s c r i p t s a n d P H P s e r v e r - s i d e s c r i p t s p

    U s e r t r a c k i n g ; t h a t i s , s e s s i o n m a n a g e m e n t t e c h

    U s e r a u t h e n t i c a t i o n a n d m a n a g e m e n t

    R e p o r t i n g

    L e t ' s t a k e a l o o k a t t h e s c o p e o f t h e w i n e s t o r e a n d( T h e p r o c e s s o f m o d e l i n g t h e s e r e q u i r e m e n t s w i t h( E R ) m o d e l i n g a n d c o n v e r t i n g t h i s m o d e l t o S Q L sA p p e n d i x C .T h e c o m p l e t e d w i n e s t o r e E R m o d e l a n d t h e S Q L s

    b e f o u n d i nC h a p t e r 3 . W e u s e t h e w i n e s t o r e c o m p o n e n t s a sC h a p t e r 4 . C o m p l e t e d c o m p o n e n t s o f t h e w i n e s t o r e a p pC h a p t e r1 0 t oC h a p t e r 1 3 . )

    1 . 5 . 1 W h a t I s H u g h a n d D a v e ' s O n l i n e W i n e s ?

    H u g h a n d D a v e ' s O n l i n e W i n e s i s a f i c t i o n a l o n l i n ed e t a i l t h e a i m s a n d s c o p e o f t h e w i n e s t o r e a n d t h ed e r i v e d f r o m t h e s e . W e a l s o i n t r o d u c e t h e t e c h n i c at o t h e c h a p t e r s i n t h e b o o k w h e r e t h e s e c o m p o n e nd e t a i l . W e c o n c l u d e

    i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a s e g o t o h t

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    46/831

    w i t h a d i s c u s s i o n o f t h e s h o r t c o m i n g s o f t h e w i n e s tT h e c o m p l e t e d w i n e s t o r e d e s c r i b e d i n t h i s s e c t i o n cs i t e .

    T h e w i n e s t o r e i s o p e n t o t h e p u b l i c : a n o n y m o u s u s

    a n d u s e r s c a n m a k e p u r c h a s e s i f t h e y b e c o m e m e ms i m p l e , a n d u s a b l e ; h o w e v e r , s i n c e i t w a s d e s i g n e dt o m a k e i t a t t r a c t i v e ! I t s u c c e e d s b e t t e r i n i t s t e c h n1 , 0 0 0 w i n e s , s t o c k i n f o r m a t i o n , a n d a d a t a b a s e o fo r d e r s .

    A n y u s e r w i t h a w e b b r o w s e r c a n a c c e s s t h e s i t e ,s t o c k , a n d v i e w t h e d e t a i l s . T h e d e t a i l s o f w i n e s i n

    t y p e , g r a p e v a r i e t i e s , a n d , i n s o m e c a s e s , a n e x p e rc a n a d d s e l e c t e d w i n e s t o a s h o p p i n g c a r t . U s e r s cm e m b e r s h i p a p p l i c a t i o n p r o c e s s c o l l e c t s d e t a i l s a b om o s t o n l i n e s i t e s .

    T o p u r c h a s e w i n e s , u s e r s m u s t l o g i n u s i n g t h e i r m j o i n e d a s a m e m b e r , h e i s l o g g e d i n a u t o m a t i c a l l y .u s e r c a n p l a c e a n o r d e r . A n o r d e r i s s h i p p e d i m m e

    B e h i n d t h e s c e n e s , t h e s y s t e m a l s o a l l o w s t h e s t o cs h i p m e n t s o f w i n e s t o t h e d a t a b a s e . T h e w e b s i t e mw i n e r i e s , w i n e r y r e g i o n s , a n d o t h e r i n f o r m a t i o n t o ta r e a v a i l a b l e .

    1 . 5 . 1 . 1 S y s t e m r e q u i r e m e n t s

    T h e f o l l o w i n g r e q u i r e m e n t s c a n t y p i c a l l y b e g a t h ei n t e r v i e w s , a n d s o o n . B u t , o f c o u r s e , t h i s b o o k i s na n d w e p r e s e n t h e r e t h e g e n e r a l r e q u i r e m e n t s t h a tb o o k . S o m e a s p e c t s o f o u r r e q u i r e m e n t s a r e s i m p l is t o r e a r e o m i t t e d , a n d s o m e d e t a i l s a r e r e a l - w o r l d a

    T h e r e q u i r e m e n t s l i s t e d h e r e a r e a n o v e r v i e w ; a r ep r e s e n t t h e s e f a c t s i n d e t a i l e d f u n c t i o n a l a n d s y s t e ma p p l i c a t i o n w o u l d a l s o h a v e a n a c c o m p a n y i n g d e s i gd e s i g n , s c r e e n l a y o u t s , a n d i n f o r m a t i o n f l o w s .

    i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a s e g o t o h t

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    47/831

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    48/831

    o r d e r o f t h e s e g r a p e v a r i e t i e s i s i m p o r t a n t . F o rv a r i e t i e s , C a b e r n e t a n d M e r l o t , a C a b e r n e t M e rC a b e r n e t .

    U s e r s m a y b r o w s e w i n e s a t t h e w i n e s t o r e b y t y

    W i n e s a r e p r o d u c e d b y o n e w i n e r y .

    W i n e r i e s h a v e a d e s c r i p t i o n w h i c h i s t y p i c a l l yf a x n u m b e r .

    W i n e r i e s a r e i n o n e r e g i o n . A r e g i o n i s a n a r e aS o u t h A u s t r a l i a a n d e a c h r e g i o n h a s a d e s c r i p

    o f t h e a r e a .

    A s h o p p i n g b a s k e t i s a n i n c o m p l e t e o r d e r t h a tt o a c o m p l e t e d o r d e r a f t e r t h e u s e r l o g s i n . E a cw i n e , a q u a n t i t y o f t h a t w i n e t o b e p u r c h a s e d ,t h e w i n e i s a l w a y s t h e p r i c e o f t h e f i r s t b o t t l e ow h i c h i n t u r n i s a l w a y s t h e c h e a p e s t a v a i l a b l e i

    T h e q u a n t i t i e s o f w i n e s i n t h e s h o p p i n g b a s k e ti t e m s c a n b e r e m o v e d f r o m t h e s h o p p i n g b a s k e

    T h e w i n e s a v a i l a b l e f o r s a l e a r e s t o r e d i n a n i nd a t e a d d e d a n d i s f o r a p a r t i c u l a r w i n e . T h e i n va v a i l a b l e a t a p a r t i c u l a r p e r - b o t t l e a n d p e r - c a s ei n v e n t o r y r e c o r d s f o r a w i n e , r e p r e s e n t i n g d i f f e rw i n e s t o r e o n d i f f e r e n t d a t e s o r t h a t h a v e a d i f f e

    T h e u s e r w i l l a l w a y s b e a d v e r t i s e d p r i c e s f r o mw i n e . W h e n a u s e r a d d s a w i n e t o h e r s h o p p i np r i c e .

    A u s e r c a n p u r c h a s e o n l y w i n e s t h a t a r e i n s t o

    W h e n a u s e r c o n v e r t s h i s s h o p p i n g b a s k e t t o ai n v e n t o r y t o c o m p l e t e t h e o r d e r i s c h e c k e d . I f ii s a l e r t e d , a n d t h e q u a n t i t i e s i n t h e s h o p p i n g b ao c c u r i f a u s e r a d d s m o r e w i n e t o h i s b a s k e t t h

    i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a s e g o t o h t

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    49/831

    W h e n s u f f i c i e n t i n v e n t o r y i s a v a i l a b l e t o c o m p l et h e i n v e n t o r y i s r e d u c e d a s t h e o r d e r i s f i n a l i z et h e o l d e s t i n v e n t o r y o f t h a t w i n e .

    1 . 5 . 2 C o m p o n e n t s o f t h e W i n e s t o r e

    T h i s s e c t i o n o u t l i n e s w h e r e t h e p r i n c i p l e s a n d p r ac o m p o n e n t o f t h e w i n e s t o r e a r e c o v e r e d t h r o u g h o u ta p p l i c a t i o n i s t h e s u b j e c t o fC h a p t e r 1 0 t h r o u g hC h a p t e r 1 3 .

    1 . 5 . 2 . 1 D a t a b a s e - d r i v e n q u e r y i n g

    I nC h a p t e r 4 , w e i n t r o d u c e t h e t e c h n i q u e s t o c o n n e c t t oc o m p l e x S Q L q u e r y , r e t r i e v e r e s u l t s , a n d p r o c e s s t ht e c h n i q u e s , w e i m p l e m e n t t h e H o t N e w W i n e s p a n eT h e c o m p l e t e d p a n e l i s s h o w n i nF i g u r e 1 - 5 . T h e p a n e l s h o w s t h e n e w e sa d d e d t o t h e d a t a b a s e t h a t h a v e b e e n r e v i e w e d b yc a r t c o m p o n e n t i s d e s c r i b e d i nC h a p t e r 1 1 a n d i n c l u d e s t h e p a n e l c o d eC h a p t e r 4 .

    F i g u r e 1 - 5 . T h e c o m p l e t e d f r o n t p a g e p a n e l w i t h t h e H o t N e w W i n

    i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a s e g o t o

  • 8/7/2019 (Oreilly) Web Database Applications With Php and Mysql (Chm)

    50/831

    1 . 5 . 2 . 2 U s e r - d r i v e n q u e r y i n g a n d b r o w s i n g

    U s e r s c a n d i s p l a y s e l e c t e d w i n e s s t o c k e d a t t h e wc r i t e r i a .

    T h e r e s u l t o f c l i c k i n g S e a r c h a f t e r s e l e c t i n g w i n e sr e g i o n i s s h o w n i nF i g u r e 1 - 6 . T h e r e s u l t s s c r e e n s h o w s t h e f i r s tt h e c r i t e r i a a n d h a s l i n k s a t t h e b a s e o f t h e s c r e e nr e s u l t s .

    i s d o c u m e n t w a s c r e a t e d b y a n u n r e g i s t e r e d C h m M a g i c , p l e a s e g o t o h t

  • 8/7/2019 (O