Posts Tagged ‘mysql’

Display MySQL query output on the command line

Tuesday, January 19th, 2010

If you are using the MySQL command line client regularly, the following scenario might be familiar to you: You run a select on a table, which contains a lot of fields. The output will not fit on one line, which means that it will be unreadable. Consider the following example:

  1. mysql> SELECT * FROM posts LIMIT 1;
  2. +----+-------------+---------------------+---------------------+--------------------------------+--------------+-----------
  3. ----+--------------+----------+----------+-------------+----------------+-------------+---------------+-------------+----
  4. -----+--------+---------------------+---------------------+-----------------------+-------------+-------+------------+----
  5. -------+----------------+---------------+
  6. | ID | post_author | post_date | post_date_gmt | post_content | post_title |
  7. post_category | post_excerpt | post_lat | post_lon | post_status | comment_status | ping_status |
  8. post_password | post_name | to_ping | pinged | post_modified | post_modified_gmt |
  9. post_content_filtered | post_parent | guid | menu_order | post_type | post_mime_type |
  10. comment_count |

  11. +----+-------------+---------------------+---------------------+--------------------------------+--------------+-----------
  12. ----+--------------+----------+----------+-------------+----------------+-------------+---------------+-------------+----
  13. -----+--------+---------------------+---------------------+-----------------------+-------------+-------+------------+----
  14. -------+----------------+---------------+

  15. | 1 | 1 | 2004-01-17 18:17:37 | 2004-01-17 16:17:37 | We'll see what time brings ... | Hello world! | 1
  16. | | NULL | NULL | publish | closed | closed | | hello-world
  17. | | | 2004-01-17 18:17:37 | 2004-01-17 16:17:37 | | 0 | /?p=1
  18. | 0 | post | | 0 |

  19. +----+-------------+---------------------+---------------------+--------------------------------+--------------+-----------
  20. ----+--------------+----------+----------+-------------+----------------+-------------+---------------+-------------+----
  21. -----+--------+---------------------+---------------------+-----------------------+-------------+-------+------------+----
  22. -------+----------------+---------------+
  23. 1 row in set (0.00 sec)

There is actually a nice solution for this. By default, the command line client uses horizontal mode to display the results of MySQL queries. If you use \G instead of ; or \g at the end of the line, your query result will be displayed in vertical mode, which will look a lot better:

  1. mysql> SELECT * FROM posts LIMIT 1\G
  2. *************************** 1. row ***************************
  3. ID: 1
  4. post_author: 1
  5. post_date: 2004-01-17 18:17:37
  6. post_date_gmt: 2004-01-17 16:17:37
  7. post_content: We'll see what time brings ...
  8. post_title: Hello world!
  9. post_category: 1
  10. post_excerpt:
  11. post_lat: NULL
  12. post_lon: NULL
  13. post_status: publish
  14. comment_status: closed
  15. ping_status: closed
  16. post_password:
  17. post_name: hello-world
  18. to_ping:
  19. pinged:
  20. post_modified: 2004-01-17 18:17:37
  21. post_modified_gmt: 2004-01-17 16:17:37
  22. post_content_filtered:
  23. post_parent: 0
  24. guid: /?p=1
  25. menu_order: 0
  26. post_type: post
  27. post_mime_type:
  28. comment_count: 0
  29. 1 row in set (0.00 sec)

While vertical output uses a lot more vertical space than the horizontal output, the vertical output is a lot more readable. Now you can probably see what I used as example query. It is actually the first post from this blog. Yes, this blog just got 6 years old ;).

If you want your command line client to use vertical mode by default, you can add vertical to the client section ([client]) of your .my.cnf file. This would look something like this:

  1. [client]
  2. vertical

Storing binary data inside a MySQL database

Tuesday, April 28th, 2009

Sometimes you need to store binary data inside a MySQL database. While there are various field types for binary data in MySQL, its handling of binary data has a share of different problems. As far as I know you might hit some bugs, if you’re using non-standard connection encodings or character sets. Also, binary data is a hell to escape properly. For example, if you store S????'x7?ma?X?UHMh? inside the MySQL database you might end up with ?????r??v ???????v later under certain conditions. This is not really ideal, to say the least.

The best thing to do is to convert the binary data to a string-based representation, e.g. a hexadecimal form or use a two-way encryption method like Base64. In fact, I recommend to use Base64. The drawback is, that a Base64 representation uses around 35% more space than the original binary representation. For example, in PHP you would do something like this:

  1. // Encode binary data
  2. $binary = base64_encode($binary);
  3.  
  4. // Decode binary data
  5. $binary = base64_decode($binary);

Update: Changed the example data slightly to accommodate some screen readers, that require a valid feed. Thank you Jan for pointing this out.