Displaying Query Results as Paragraph Text

17.2.1 Problem

You want to display a query result as free text.

17.2.2 Solution

Display it with no surrounding HTML structure other than paragraph tags.

17.2.3 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

and

tags. The following examples show how to produce a paragraph for a status display that includes the current date and time, the server version, the client username, and the current database name (if any). These values are available from the following query:

mysql> SELECT NOW( ), VERSION( ), USER( ), DATABASE( );
+---------------------+-----------------+----------------+------------+
| NOW( ) | VERSION( ) | USER( ) | DATABASE( ) |
+---------------------+-----------------+----------------+------------+
| 2002-05-18 11:33:12 | 4.0.2-alpha-log | paul@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);
$para = <

In PHP, you can print the

and

tags around the encoded paragraph text:

$query = "SELECT NOW( ), VERSION( ), USER( ), DATABASE( )";
$result_id = mysql_query ($query, $conn_id);
if ($result_id)
{
 list ($now, $version, $user, $db) = mysql_fetch_row ($result_id);
 mysql_free_result ($result_id);
 if (!isset ($db))
 $db = "NONE";
 $para = "Local time on the MySQL server is $now."
 . " The server version is $version."
 . " The current user is $user."
 . " The current database is $db.";
 print ("

" . htmlspecialchars ($para) . "

"); }

Or, after fetching the query result, you can print the paragraph by beginning in HTML mode and switching between modes:


 

Local time on the MySQL server is . The server version is . The current user is . The current database is .

To display a paragraph in Python, do something like this::

cursor = conn.cursor ( )
cursor.execute ("SELECT NOW( ), VERSION( ), USER( ), DATABASE( )")
row = cursor.fetchone ( )
if row is not None:
 if row[3] is None: # check database name
 row[3] = "NONE"
 para = ("Local time on the MySQL server is %s." +
 " The server version is %s." +
 " The current user is %s." +
 " The current database is %s.") % (row)
 print "

" + cgi.escape (para, 1) + "

" cursor.close ( )

In JSP, the display might be produced as follows:

 SELECT NOW( ), VERSION( ), USER( ), DATABASE( )




 

Local time on the server is . The server version is . The current user is . The current database is .

The JSP script uses rowsByIndex so that the result set row's columns can be accessed by numeric index.

Using the mysql Client Program

Writing MySQL-Based Programs

Record Selection Techniques

Working with Strings

Working with Dates and Times

Sorting Query Results

Generating Summaries

Modifying Tables with ALTER TABLE

Obtaining and Using Metadata

Importing and Exporting Data

Generating and Using Sequences

Using Multiple Tables

Statistical Techniques

Handling Duplicates

Performing Transactions

Introduction to MySQL on the Web

Incorporating Query Resultsinto Web Pages

Processing Web Input with MySQL

Using MySQL-Based Web Session Management

Appendix A. Obtaining MySQL Software

Appendix B. JSP and Tomcat Primer

Appendix C. References



MySQL Cookbook
MySQL Cookbook
ISBN: 059652708X
EAN: 2147483647
Year: 2005
Pages: 412
Authors: Paul DuBois

Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net