Client software packages presenting the illusion of "plain text" from query result sets actually do a bunch of formatting work to render the result that way.
They rely on the fact that the RDMS returns column descriptors (metadata) with the data. In PHP's mysqli interface, for example, the fetch_fields method retrieves the column descriptors. Each column has a bunch of items describing it, including the maximum character width it needs.
To render a result set as text, you need to use this metadata to format it.
See here for an explanation. http://www.php.net/manual/en/mysqli-result.fetch-fields.php