How to convert MySQL output to HTML tables

In this article I’ll explain how to control the output of the mysql client program and feed it to another program to transform the results as desired. I often transform output into HTML tables for these blog articles — at least, I do when I’m not being lazy.

The mysql command-line program can accept a command and print the results directly to STDOUT. The default output behavior differs depending on where its input comes from. When the input comes via STDIN, the output is tab-separated values. When the input comes via the -e or --execute options, or when the -t option is specified, the output is in “tabular” format, with borders drawn by pipes, dashes and plus characters.

Results can also be printed vertically, as they are when an interactive command is terminated with \G instead of a semicolon. The command-line option for this format is -E or --vertical.

I usually select results in non-tabular format and feed them to awk to turn them into HTML. Here is a quick one-liner that will format the first two columns:

echo "select ..." | mysql | sed -e 's/\|//g' | awk '{a++; if(a<2){print "<tr><th>" $1 "</th><th>" $2 "</th></tr>";} else { print "<tr><td>" $1 "</td><td>" $2 "</td></tr>"; }}'

The result is formatted into HTML rows and columns, and all I need to do is wrap it in a set of <table> tags.

I sometimes use Perl, too. Here’s a script I’ve saved in my PATH so I can pipe results into it:

#!/usr/bin/perl
use strict;
use warnings;

LINE:
while (my $line = <STDIN>) {
    next LINE if $line =~ m/^\+/;
    chomp $line;
    if ($line =~ m/^\|/) {
        $line =~ s#^\| | \|$##g;
        chomp $line;
        print "<tr><td>"
            . join("</td><td>", split(/(?<=\S)\s+\|\s+(?=\S)/, $line))
            . "</td></tr>\n";
    }
    else {
        print "<tr><td>"
            . join("</td><td>", split(/\t/, $line))
            . "</td></tr>\n";
    }
}

Update Looks like the MySQL folks already did this work for me, duh. The -H option outputs HTML for query results.

Technorati Tags:No Tags

You might also like:

  1. What does “> /dev/null 2>&1″ mean?
  2. How to create a VB6 console program
  3. MySQL Toolkit released as one package
  4. MySQL Toolkit’s Show Grants tool 0.9.1 released
  5. MySQL Toolkit version 815 released

1 Response to “How to convert MySQL output to HTML tables”


  1. 1 Amr Hamdy

    Thanks, man..
    Your posts helped me too much specially when I read about the “-H” things …
    Thanks too much :)

Leave a Reply

Please do not use this blog to get help with problems or bugs in Maatkit or innotop: use the appropriate forums, mailing list, or bug trackers. If you're asking for help with MySQL, please use the MySQL mailing list instead.