In MySQL, the FROM_BASE64() function decodes a base-64 encoded string and returns the result. More specifically, it takes a string encoded with the base-64 encoded rules used by TO_BASE64() and returns the decoded result as a binary string.
Syntax
The syntax goes like this:
FROM_BASE64(str)
Where str is the base-64 encoded string that you want decoded.
Example 1 – Basic Usage
Here’s an example to demonstrate the basic usage:
SELECT FROM_BASE64('Q2F0');
Result:
+---------------------+
| FROM_BASE64('Q2F0') |
+---------------------+
| Cat |
+---------------------+
So in this example, our argument is Q2F0, which is the base-64 encoded string for Cat.
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’s an example using a longer string:
SELECT FROM_BASE64('TXkgY2F0IGxpa2VzIHRvIGNoYXNlIGVsZXBoYW50cyE=');
Result:
+-------------------------------------------------------------+
| FROM_BASE64('TXkgY2F0IGxpa2VzIHRvIGNoYXNlIGVsZXBoYW50cyE=') |
+-------------------------------------------------------------+
| My cat likes to chase elephants! |
+-------------------------------------------------------------+
Example 3 – Invalid Argument
If the argument is not a valid base-64 string, NULL will be returned:
SELECT FROM_BASE64('Oops!');
Result:
+----------------------+
| FROM_BASE64('Oops!') |
+----------------------+
| NULL |
+----------------------+
Example 4 – NULL Argument
You’ll also get NULL if you pass in NULL:
SELECT FROM_BASE64(NULL);
Result:
+-------------------+ | FROM_BASE64(NULL) | +-------------------+ | NULL | +-------------------+
Example 5 – Missing Argument
You’ll get an error if you don’t pass in an argument:
SELECT FROM_BASE64();
Result:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'FROM_BASE64'
Example 6 – Too Many Arguments
You’ll also get an error if you pass in too many arguments:
SELECT FROM_BASE64('Q2F0', 'RWxlcGhhbnQ=');
Result:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'FROM_BASE64'