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.")); print p (escapeHTML ("The current user is $user.")); print p (escapeHTML ("The default database is $db."));
In Ruby, use the cgi
module
escapeHTML
method to encode the paragraph
text, and then pass it to the p
method to produce the paragraph tags:
(now, version, user, db) = dbh.select_one("SELECT NOW(), VERSION(), USER(), DATABASE()") db = "NONE" if db.nil? cgi = CGI.new("html4") cgi.out { cgi.p { CGI.escapeHTML("Local time on the MySQL server is #{now}.") } + cgi.p { CGI.escapeHTML("The server version is #{version}.") } + cgi.p { CGI.escapeHTML("The current user is #{user}.") } + cgi.p { CGI.escapeHTML("The default database is #{db}.") } }
In PHP, put <p>
and
</p>
tags around the encoded
paragraph text:
$result =& $conn->query ("SELECT NOW(), VERSION(), USER(), DATABASE()"); if (!PEAR::isError ($result)) { list ($now, $version, $user, $db) = $result->fetchRow (); $result->free (); if (!isset ($db)) $db = "NONE"; $para = "Local time on the MySQL server is $now."; print ("<p>" . htmlspecialchars ($para) . "</p> "); $para = "The server version is $version."; print ("<p>" . htmlspecialchars ($para) . "</p> "); $para = "The current user is $user."; print ("<p>" . htmlspecialchars ($para) . "</p> "); $para = "The default database is $db."; print ("<p>" . htmlspecialchars ($para) . "</p> "); }
Or, after fetching the query result, you can print the paragraph by beginning in HTML mode and switching between modes:
<p>Local time on the MySQL server is <?php print (htmlspecialchars ($now)); ?>.</p> <p>The server version is <?php print (htmlspecialchars ($version)); ?>.</p> <p>The current user is <?php print (htmlspecialchars ($user)); ?>.</p> <p>The default database is <?php print (htmlspecialchars ($db)); ?>.</p>
To display paragraphs in Python, do something like this:
cursor = conn.cursor () cursor.execute ("SELECT NOW(), VERSION(), USER(), DATABASE()") (now, version, user, db) = cursor.fetchone () cursor.close () if db is None: # check database name db = "NONE" para = ("Local time on the MySQL server is %s.") % now print "<p>" + cgi.escape (para, 1) + "</p>" para = ("The server version is %s.") % version print "<p>" + cgi.escape (para, 1) + "</p>" para = ("The current user is %s.") % user print "<p>" + cgi.escape (para, 1) + "</p>" para = ("The default database is %s.") % db print "<p>" + cgi.escape (para, 1) + "</p>"
In JSP, the paragraph display can be produced as follows, using
rowsByIndex
to access the result
set row’s columns by numeric index and
<c:out>
to
encode and print the text:
<sql:query dataSource="${conn}" var="rs"> SELECT NOW(), VERSION(), USER(), DATABASE() </sql:query> <c:set var="row" value="${rs.rowsByIndex[0]}"/> <c:set var="db" value="${row[3]}"/> <c:if test="${empty db}"> <c:set var="db" value="NONE"/> </c:if> <p>Local time on the server is <c:out value="${row[0]}"/>.</p> <p>The server version is <c:out value="${row[1]}"/>.</p> <p>The current user is <c:out value="${row[2]}"/>.</p> <p>The default database is <c:out value="${db}"/>.</p>
Using a Template System to Generate Web Pages discusses how to generate paragraphs using templates.
3.138.106.233