Introduction Incorporating Query Results into Web Pages

17.1 Introduction

When you st ore inform at ion in your dat abase, you can easily ret rieve it for use on t he Web in a variet y of ways. Query result s can be displayed as unst ruct ured paragraphs or as st ruct ured elem ent s such as list s or t ables; you can display st at ic t ext or creat e hyperlinks. Query m et adat a can be useful when form at t ing query result s, t oo, such as when generat ing an HTML t able t hat displays a result set and uses it s m et adat a t o get t he colum n headings for t he t able. These t asks com bine query processing wit h web script ing, and are prim arily a m at t er of properly encoding any special charact ers in t he result s like or and adding t he appropriat e HTML t ags for t he t ypes of elem ent s you want t o produce. This chapt er shows how t o generat e several t ypes of web out put from query result s. I t also covers t echniques for insert ing binary dat a int o your dat abase and for ret rieving and t ransferring t hat kind of inform at ion t o client s. I t s easiest and m ost com m on t o work wit h t ext for creat ing web pages from dat abase cont ent , but you can also use MySQL t o help service request s for binary dat a such as im ages, sounds, or PDF files. The recipes here build on t he t echniques shown in Chapt er 16 for generat ing web pages from script s and for encoding out put for display. See t hat chapt er if you need som e background in t hese t opics. The script s from which t he exam ples in t his chapt er are drawn can be found in t he recipes dist ribut ion 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 Java JSP exam ples, look under t om cat ; you should already have inst alled t hese in t he process of set t ing up t he mcb applicat ion cont ext Recipe 16.4 . The except ion t o t his is t hat som e of t he ut ilit y rout ines used here are found in library files in t he lib direct ory. Not e t hat not all languages are represent ed in every sect ion. I f a part icular sect ion seem s t o be m issing an exam ple for t he language youre int erest ed in, check t he recipes dist ribut ion; it m ay cont ain t he im plem ent at ion you want , even if it s not shown here.

17.2 Displaying Query Results as Paragraph Text