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

How to decode base64 encoded string in MySQL?

藏色散人
Release: 2021-02-10 09:55:49
Original
11522 people have browsed it

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)
Copy after login

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;);
Copy after login

Result:

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

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;);
Copy after login

Result:

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

Example 2 - A longer string

Here is an example using a longer string:

SELECT FROM_BASE64(&#39;TXkgY2F0IGxpa2VzIHRvIGNoYXNlIGVsZXBoYW50cyE=&#39;);
Copy after login

Result:

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

Example 3 - Invalid Parameter

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

SELECT FROM_BASE64(&#39;Oops!&#39;);
Copy after login

Result:

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

Example 4 - NULL parameter

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

SELECT FROM_BASE64(NULL);
Copy after login

result :

+-------------------+
| FROM_BASE64(NULL) |
+-------------------+
| NULL              |
+-------------------+
Copy after login

Example 5 - Missing Parameter

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

SELECT FROM_BASE64();
Copy after login

Result:

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

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;);
Copy after login

Result:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template