Formatting the results of a MySQL query as if it w

2019-02-12 06:41发布

I'm writing a quick and dirty reporting script that queries a report and emails the results. When using the MySQL console the results are in a nicely formatted table:

mysql> select * from users;
+-----------+------------+-------+
| firstname | city       | zip   |
+-----------+------------+-------+
| Maria     | Holland    | 12345 |
| Rene      | Doylestown | 65432 |
| Helen     | Conway     | 98745 |
+-----------+------------+-------+
3 rows in set (0.01 sec)

Is there an easy way to replicate this formatting when fetching the results with PHP? Obviously I could achieve this by writing my own report formatter but I was hoping for something a little more elegant.

7条回答
乱世女痞
2楼-- · 2019-02-12 07:42

You could use exec or backticks and actually run it from the command line through php. Apparently the mysql command has a -H switch you can use, and it will output HTML formatted. Haven't tried it though, but that might look good too.

echo '<pre>';
echo `mysql -u user -ppass -e "select * from table;" database_name`;

2 lines, no pear packages, how much more elegant can it get for a quick and dirty stats page.

查看更多
登录 后发表回答