Home  >  Article  >  Database  >  How to decode base64 encoded string in MySQL?

How to decode base64 encoded string in MySQL?

藏色散人
藏色散人Original
2019-03-29 11:40:3111343browse

This article mainly introduces how to decode base64 encoded strings in mysql, then we can achieve decoding through the FROM_BASE64() function.

How to decode base64 encoded string in MySQL?

In MySQL, the FROM_BASE64() function decodes a base-64 encoded string and returns the result. More specifically, it accepts a string encoded with the base-64 encoding rules used by TO_BASE64() and returns the decoded result as a binary string.

<strong>FROM_BASE64()</strong>The syntax is as follows:

FROM_BASE64(str)

The parameter str is the base-64 encoding you wish to decode String.

Example 1 - Basic usage

The following is an example to demonstrate basic usage:

SELECT FROM_BASE64(&#39;Q2F0&#39;);

Result:

+---------------------+
| FROM_BASE64(&#39;Q2F0&#39;) |
+---------------------+
| Cat                 |
+---------------------+

In this example, our parameter is Q2F0, which is Cat's base-64 encoded string.

We can get the base-64 encoded string by passing Cat to the TO_BASE64() function:

SELECT TO_BASE64(&#39;Cat&#39;);

Result:

+------------------+
| TO_BASE64(&#39;Cat&#39;) |
+------------------+
| Q2F0             |
+------------------+

Example 2 - A longer string

Here is an example using a longer string:

SELECT FROM_BASE64(&#39;TXkgY2F0IGxpa2VzIHRvIGNoYXNlIGVsZXBoYW50cyE=&#39;);

Result:

+-------------------------------------------------------------+
| FROM_BASE64(&#39;TXkgY2F0IGxpa2VzIHRvIGNoYXNlIGVsZXBoYW50cyE=&#39;) |
+-------------------------------------------------------------+
| My cat likes to chase elephants!                            |
+-------------------------------------------------------------+

Example 3 - Invalid Parameter

If the parameter is not a valid base-64 string, return NULL:

SELECT FROM_BASE64(&#39;Oops!&#39;);

Result:

+----------------------+
| FROM_BASE64(&#39;Oops!&#39;) |
+----------------------+
| NULL                 |
+----------------------+

Example 4 - NULL parameter

If you pass in NULL, you will also get the NULL:

SELECT FROM_BASE64(NULL);

result :

+-------------------+
| FROM_BASE64(NULL) |
+-------------------+
| NULL              |
+-------------------+

Example 5 - Missing Parameter

If you don't pass a parameter, you will get an error:

SELECT FROM_BASE64();

Result:

ERROR 1582 (42000): Incorrect parameter count in the call to native function &#39;FROM_BASE64&#39;

Example 6 - Too Many Parameters

If you pass in too many parameters, you will also get an error:

SELECT FROM_BASE64(&#39;Q2F0&#39;, &#39;RWxlcGhhbnQ=&#39;);

Result:

ERROR 1582 (42000): Incorrect parameter count in the call to native function &#39;FROM_BASE64&#39;

This article is about the method of decoding base64 encoded strings in MySQL. I hope it will be helpful to friends in need!

The above is the detailed content of How to decode base64 encoded string in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn