Introduction Introduction to MySQL on the Web

16.1 Introduction

The next few chapt ers discuss som e of t he ways t hat MySQL can help you build a bet t er web sit e. I n general, t he principal benefit is t hat MySQL m akes it easier t o provide dynam ic rat her t han st at ic cont ent . St at ic cont ent exist s as pages in t he web servers docum ent t ree t hat are served exact ly as is. Visit ors can access only t he docum ent s t hat you place in t he t ree, and changes occur only when you add, m odify, or delet e t hose docum ent s. By cont rast , dynam ic cont ent is creat ed on dem and. Rat her t han opening a file and serving it s cont ent s direct ly t o t he client , t he web server execut es a script t hat generat es t he page and sends t he result ing out put . As a sim ple exam ple, a script can look up t he current hit count er value in t he dat abase for a given web page, updat e t he count er, and ret urn t he new value for display in t he page. Each t im e t he script execut es, it produces a different value. More com plex exam ples are script s t hat show t he nam es of people t hat have a birt hday t oday, ret rieve and display it em s in a product cat alog, or provide inform at ion about t he current st at us of t he server. And t hat s j ust for st art ers; web script s have access t o t he power of t he program m ing language in which t heyre writ t en, so t he act ions t hat t hey perform t o generat e pages can be quit e ext ensive. For exam ple, web script s are im port ant for form processing, and a single script m ay be responsible for generat ing a form and sending it t o t he user, processing t he cont ent s of t he form when t he user subm it s it lat er, and st oring t he cont ent s in a dat abase. By com m unicat ing wit h users t his way, web script s bring a m easure of int eract ivit y t o your web sit e. This chapt er covers t he int roduct ory aspect s of writ ing script s t hat use MySQL in a web environm ent . Som e of t he init ial m at erial is not part icularly MySQL- specific, but it is necessary t o est ablish t he general groundwork for using your dat abase from wit hin t he cont ext of web program m ing. The t opics covered here include: • How web script ing differs from writ ing st at ic HTML docum ent s or script s int ended t o be execut ed from t he com m and line. • Som e of t he prerequisit es for running web script s. I n part icular, you m ust have a web server inst alled and it m ust be set up t o recognize your script s as program s t o be execut ed, rat her t han as st at ic files t o be served lit erally over t he net work. • How t o use each of our API languages t o writ e a short web script t hat queries t he MySQL server for inform at ion and displays t he result s in a web page. • How t o properly encode out put . HTML consist s of t ext t o be displayed int erspersed wit h special m arkup const ruct s. However, if t he t ext cont ains special charact ers, you m ust encode t hem t o avoid generat ing m alform ed web pages. Each API provides a m echanism for doing t his. The follow ing chapt ers go int o m ore det ail on t opics such as how t o display query result s in various form at s paragraphs, list s, t ables, and so fort h , working wit h im ages, form processing, and t racking a user across t he course of several page request s as part of a single user session. This book uses t he Apache web server for Perl, PHP, and Pyt hon script s, and t he Tom cat server for Java script s—writ t en using JavaServer Pages JSP not at ion. Bot h servers are available from t he Apache Group: ht t p: ht t pd.apache.org ht t p: j akart a.apache.org Because Apache inst allat ions are fairly prevalent , I m going t o assum e t hat it is already inst alled on your syst em and t hat you j ust need t o configure it . Recipe 16.3 discusses how t o configure Apache for Perl, PHP, and Pyt hon, and how t o writ e a short web script in each language. Tom cat is less widely deployed t han Apache, so som e addit ional inst allat ion inform at ion is provided in Appendix B . Recipe 16.4 discusses JSP script writ ing using Tom cat . You can use different servers if you like, but youll need t o adapt t he inst ruct ions given here. The w eb-based exam ple script s in t he recipes dist ribut ion m ay be found under t he direct ories nam ed for t he servers used t o run t hem . For Perl, PHP, and Pyt hon exam ples, look under t he apache direct ory; for JSP exam ples, look under t om cat . I will assum e t hat you have som e basic fam iliarit y wit h HTML. For Tom cat , it s also helpful t o know som et hing about XML, because Tom cat s configurat ion files are writ t en as XML docum ent s, and JSP pages cont ain elem ent s writ t en using XML synt ax. I f you dont know any XML, see t he quick sum m ary in t he sidebar XML and XHTML in a Nut shell. I n general, t he web script s in t his book produce out put t hat is valid not only as HTML, but as XHTML, t he t ransit ional form at bet ween HTML and XML. This is anot her reason t o becom e fam iliar wit h XML. For exam ple, XHTML requires closing t ags, so paragraphs are writ t en wit h a closing p t ag following t he paragraph body. The use of t his out put st yle will be obvious for script s writ t en using languages like PHP in which t he HTML t ags are included lit erally in t he script . For int erfaces t hat generat e HTML for you, like t he Perl CGI .pm m odule, conform ance t o XHTML is a m at t er of whet her or not t he m odule it self produces XHTML. CGI .pm does so beginning wit h Version 2.69, t hough it s XHTML conform ance im proves in m ore recent versions. XML and XHTML in a Nutshell XML is sim ilar in som e ways t o HTML, and because m ore people know HTML, it s perhaps easiest t o charact erize XML in t erm s of how it differs from HTML: • Let t ercase for HTML t ag and at t ribut e nam es does not m at t er; in XML, t he nam es ar e case sensit ive. • I n HTML, t ag at t ribut es can be specified wit h a quot ed or unquot ed value, or som et im es wit h no value at all. I n XML, every t ag at t ribut e m ust have a value, and t he value m ust be quot ed. • Every opening t ag in XML m ust have a corresponding closing t ag. This is t rue even if t here is no body, alt hough in t hat case, a short cut t ag form can be used. For exam ple, in HTML, you can writ e br , but XML r equir es a closing t ag. You could w rit e t his as brbr , but t he elem ent has no body, so a short cut form br can be used t hat com bines t he opening and closing t ags. However, when writ ing XML t hat will be t ranslat ed int o HTML, it s safer t o writ e t he t ag as br w it h a space preceding t he slash. The space helps browsers not t o m isint erpret t he t ag nam e as br and consequent ly ignore it as unrecognized. XHTML is a t ransit ional form at used for t he m igrat ion of t he Web away from HTML and t oward XML. I t s less st rict t han XML, but m ore st rict t han HTML. For exam ple, XHTML t ag and at t ribut e nam es m ust be lowercase and at t ribut es m ust have a double-quot ed value. I n HTML you m ight writ e a radio but t on elem ent like t his: INPUT TYPE=RADIO NAME=my button VALUE=3 CHECKED I n XHTML, t he t ag nam e m ust be lowercase, t he at t ribut e values m ust be quot ed, t he checked at t ribut e m ust be given a value, and t here m ust be a closing t ag: input type=radio name=my button value=3 checked=checkedinput The elem ent has no body in t his case, so t he single- t ag short cut form can be used: input type=radio name=my button value=3 checked=checked Appendix C list s som e references if you want addit ional general inform at ion about HTML, XHTML, or XML.

16.2 Basic Web Page Generation