سؤال

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.

هل كانت مفيدة؟

المحلول

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;"
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top