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

后端 未结 7 2143
清酒与你
清酒与你 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:15

    You could do this quite easily using the Console_Table PEAR package. Just loop through your MySQL results, and add rows to your table. You can use the Console_Table::setHeaders() method to add the headers for your columns, then the Console_Table::addRow() method to add each row, and finally Console_Table::getTable() to display it.

    There is nothing built into PHP to do this. If you don't want to use/write code to draw console tables, just pass -e query to mysql via PHP using passthru(). This will work queries terminated with both ; and \G:

    passthru("mysql -e '$query;' database_name");
    

提交回复
热议问题