【发布时间】:2014-04-10 19:37:18
【问题描述】:
我正在尝试编写一个 cgi 脚本,该脚本使用 mysql 查询从 MySQL 数据库中读取数据并将数据存储在一个变量中。变量输出使用 HTML 显示
为了保留 MySQL 查询输出的原始格式,我在显示变量值时使用了“”字符,如此处所建议的。 How to get proper tabular output like the mysql command line from a bash script
但在 HTML 中显示变量的值时,看起来原始格式并未保留。
这是 MySQL 查询的输出
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)
使用 HTML 显示时,相同的输出如下所示
host db www test web test localhost bugs localhost hrm localhost ohrm localhost otrs
上面输出的HTML代码sn-p如下
#!/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>"
如何在 HTML 中保留原始输出的格式。请建议。
【问题讨论】:
-
尝试将内容 mime 类型从
text/html更改为text/plain。 -
谢谢维韦克。工作正常。
-
但是这里还有一个问题。更改内容类型 text/plain 后不处理 HTML 标记
-
是的,
text/plain指示浏览器将其视为纯文本,而不处理其中的任何 HTML。我刚刚发布了所需 HTML 输出的答案。