1

Is there a way to print the results of a MySQL query in the same format as the terminal?

Something like:

+--------+------------------------------+------+------+
| id     | name                         | year | rank |
+--------+------------------------------+------+------+
|  10920 | Aliens                       | 1986 |  8.2 |
|  17173 | Animal House                 | 1978 |  7.5 |
|  18979 | Apollo 13                    | 1995 |  7.5 |
|  30959 | Batman Begins                | 2005 | NULL |
|  46169 | Braveheart                   | 1995 |  8.3 |
| 109093 | Fargo                        | 1996 |  8.2 |

I am querying the database from php.

Any help would be appreciated. Thanks!

5
  • I should specify, is there an 'easy' way to do this without manually formatting the output. Commented May 24, 2011 at 3:34
  • You can use something like printf to format them with specific column widths. stackoverflow.com/questions/6053095/textfile-structure-tables/… This solution can be made more flexible by checking against the max length of name and using it for the column width, but that's a little too involved for me to tackle at the moment Commented May 24, 2011 at 3:38
  • use html, css, php combination and format the o/p.. Commented May 24, 2011 at 3:39
  • pass the values through a loop, use a html table, guess it depends on your definition of easy. Commented May 24, 2011 at 3:39
  • I am aware of printing the results to a table or styling it using css. I was just wondering if there was a quick way to dump the output of query with the same formatting as the terminal. The main reason is that I want to be able to compare the results of queries (seeing if they match). I figured this would be easier without any markup involved (was just going to throw the output into a <pre> tag). I'm doing this as a lesson for a class I teach. Commented May 24, 2011 at 3:50

3 Answers 3

4

Look into sprintf/printf:

echo '+--------+------------------------------+------+------+';
echo '| id     | name                         | year | rank |';
echo '+--------+------------------------------+------+------+';

foreach (...) {
    printf('| %6s | %-28s | %4s | %4s |', $row['id'], $row['name'], $row['year'], $row['rank']);
}

You'll run into problems for anything that's longer than the predefined column size though. You can make dynamic column widths that scale to the right size by first going through your values and figuring out what the longest value is. In that case, for the horizontal lines, use str_repeat. I'll leave that as exercise for you.

Sign up to request clarification or add additional context in comments.

Comments

1

Why not print using HTML? That'd be easier than ASCII

Comments

0

Technically...

$sql = "SELECT id, name, year, rank FROM Table"
echo '<pre>';
system("mysql mysql --user=$user_name --password=$your_password $db_name -q $sql")
echo '</pre>';

But please don't because it a big security issue and just considered bad.

Again, this answer depends on how specific you are defining "How MySQL does it" and "easy".

Highly suggest using something like what was mentioned in a very similar question: Print table data mysql php

or what @deceze mentioned, although using sprintf is not very flexible, it will get the job done.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.