This article mainly introduces how to decode base64 encoded strings in mysql, then we can achieve decoding through the FROM_BASE64()
function.
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('Q2F0');
Result:
+---------------------+ | FROM_BASE64('Q2F0') | +---------------------+ | 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('Cat');
Result:
+------------------+ | TO_BASE64('Cat') | +------------------+ | Q2F0 | +------------------+
Example 2 - A longer string
Here is an example using a longer string:
SELECT FROM_BASE64('TXkgY2F0IGxpa2VzIHRvIGNoYXNlIGVsZXBoYW50cyE=');
Result:
+-------------------------------------------------------------+ | FROM_BASE64('TXkgY2F0IGxpa2VzIHRvIGNoYXNlIGVsZXBoYW50cyE=') | +-------------------------------------------------------------+ | 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('Oops!');
Result:
+----------------------+ | FROM_BASE64('Oops!') | +----------------------+ | 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 'FROM_BASE64'
Example 6 - Too Many Parameters
If you pass in too many parameters, you will also get an error:
SELECT FROM_BASE64('Q2F0', 'RWxlcGhhbnQ=');
Result:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'FROM_BASE64'
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!