Formatting the results of a MySQL query as if it were run from the console

后端 未结 7 2140
清酒与你
清酒与你 2020-12-16 15:04

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:

7条回答
  •  失恋的感觉
    2020-12-16 15:09

    Building on mfonda's answer, you can really easily load the Console_Table pear package with composer now: https://packagist.org/packages/pear/console_table

    $ composer require pear/console_table

    "; #uncomment this line if running script in a browser
    
    //The class isn't namespaced so just call it directly like so:
    echo Console_Table::fromArray(
        ['column', 'headings'], 
        [
            ['1st row', 'values'], 
            ['2nd row', 'values'], 
            ['...', '...']
        ]
    );
    

    This outputs:

    +---------+----------+
    | column  | headings |
    +---------+----------+
    | 1st row | values   |
    | 2nd row | values   |
    | ...     | ...      |
    +---------+----------+
    

提交回复
热议问题