Displaying Query Results as Paragraph Text
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. In this case all you need to do is retrieve the
text to be displayed, encode it to convert special characters to the
corresponding HTML entities, and wrap each paragraph within <p> and </p> tags. The following examples show
how to produce paragraphs for a status display that includes the
current date and time, the server version, the client username, and
the default database name (if any). These values are available from
the following query:
mysql>SELECT NOW(), VERSION(), USER(), DATABASE();
+---------------------+------------+------------------+------------+
| NOW() | VERSION() | USER() | DATABASE() |
+---------------------+------------+------------------+------------+
| 2006-10-17 15:47:33 | 5.0.27-log | cbuser@localhost | cookbook |
+---------------------+------------+------------------+------------+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 you should take care of that by calling
escapeHTML():
($now, $version, $user, $db) = $dbh->selectrow_array ("SELECT NOW(), VERSION(), USER(), DATABASE()"); $db = "NONE" unless defined ($db); print p (escapeHTML ("Local time on the MySQL server is $now.")); print p (escapeHTML ("The server version is $version.")); ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access