Database Reference
In-Depth Information
run by it can find their library files, see Recipe 18.2 .) For Java (JSP) examples, look under
the tomcat directory; you should already have installed these in the process of setting
up the mcb application context (see Recipe 18.3 ).
If a particular section has no example for a language in which you're interested, check
the recipes distribution for implementations not shown here.
The scripts in this chapter are intended to be invoked from your browser after they have
been installed, but you can invoke many of them (JSP pages excepted) from the com‐
mand line to see the raw HTML they produce; see Recipe 18.2 .
19.1. Displaying Query Results as Paragraphs
Problem
You want to display a query result as free text.
Solution
Display it within paragraph tags.
Discussion
Paragraphs are useful for displaying free text with no particular structure. Retrieve the
text to be displayed, encode it to convert any special characters to the corresponding
HTML entities, and wrap each paragraph within <p> and </p> tags. The following ex‐
amples show how to produce paragraphs for a status display that includes the current
date and time, the server version, and the default database name (if any). These values
are available from the following query:
mysql> SELECT NOW(), VERSION(), DATABASE();
+---------------------+------------+------------+
| NOW() | VERSION() | DATABASE() |
+---------------------+------------+------------+
| 2013-12-22 11:29:50 | 5.6.16-log | cookbook |
+---------------------+------------+------------+
One complication is that the DATABASE() result is NULL if there is no default database.
The examples show how to handle this.
In Perl, the CGI.pm module provides a p() function that adds paragraph tags around
the string you pass to it. p() does not HTML-encode its argument, so handle that by
calling escapeHTML() :
( $now , $version , $db ) =
$dbh -> selectrow_array ( "SELECT NOW(), VERSION(), DATABASE()" );
$db = "NONE" unless defined ( $db );
print p ( escapeHTML ( "Local time on the MySQL server is $now." ));
Search WWH ::




Custom Search