1

I am trying to write a cgi script which reads data from a MySQL database using mysql query and stores the data in a variable. The variable output is displayed using HTML

To preserve the original format of MySQL Query output , I have used "" character while displaying value of a variable as suggested here . How to get proper tabular output like the mysql command line from a bash script

But it looks like the original formatting is not preserved while displaying value of the variable in HTML.

This is the output from MySQL Query

mysql -uroot -ptest -D mysql -e "select host,db from db " ;

+-----------+---------+ | host | db | +-----------+---------+ | www | test | | web | test | | localhost | bugs | | localhost | hrm | | localhost | ohrm | | localhost | otrs | +-----------+---------+ 6 rows in set (0.00 sec)

Same output looks as follows when displayed using HTML

host db www test web test localhost bugs localhost hrm localhost ohrm localhost otrs

The HTML code snippet for above output is as follows

 #!/bin/bash

 echo "Content-type: text/html"
 echo ""

 echo "<html>"
 echo "<body>"`

 var=`mysql -h 127.0.0.1 -u root -p redhat -D mysql -e "select host,db from db;"`
 echo "$var"
 echo "</body>"
 echo "</html>"

How can I preserve the format from original output in HTML . Please suggest.

3
  • Try changing the content mime type from text/html to text/plain. Commented Apr 10, 2014 at 10:42
  • But there is another issue here . The HTML tags are not processed after changing content type text/plain Commented Apr 10, 2014 at 11:14
  • Yes, text/plain instructs the browser to treat it as a plaintext and not process any HTML in it. I just posted an answer for desired HTML output. Commented Apr 10, 2014 at 11:38

1 Answer 1

2

You can add -H or --html command line parameter while doing the mysql query to get HTML table output. Use the text/html mime type with this.

Modifying your query:

mysql -h 127.0.0.1 -u root -p redhat -H -D mysql -e "select host,db from db;"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.