In MySQL, il TO_BASE64() converte una stringa in una stringa codificata in base 64 e restituisce il risultato.
Sintassi
La sintassi è questa:
TO_BASE64(str)
Dove str è la stringa che vuoi codificare.
Esempio 1 – Utilizzo di base
Ecco un esempio per dimostrare l'utilizzo di base:
SELECT TO_BASE64('Dog'); Risultato:
+------------------+
| TO_BASE64('Dog') |
+------------------+
| RG9n |
+------------------+
Quindi in questo esempio, il nostro argomento è Dog , che diventa RG9n una volta convertito in base-64.
Possiamo usare il FROM_BASE64() funzione per decodificare la stringa in base 64:
SELECT FROM_BASE64('RG9n'); Risultato:
+---------------------+
| FROM_BASE64('RG9n') |
+---------------------+
| Dog |
+---------------------+
Esempio 2:una stringa più lunga
Ecco un esempio che utilizza una stringa più lunga:
SELECT TO_BASE64('My cat chases dogs!'); Risultato:
+----------------------------------+
| TO_BASE64('My cat chases dogs!') |
+----------------------------------+
| TXkgY2F0IGNoYXNlcyBkb2dzIQ== |
+----------------------------------+
Esempio 3 – Argomenti non stringa
Se l'argomento non è una stringa, verrà prima convertito in una stringa:
SELECT TO_BASE64(123);
Risultato:
+----------------+ | TO_BASE64(123) | +----------------+ | MTIz | +----------------+
Esempio 4 – Argomento NULL
Otterrai
NULL
se passi in NULL :
SELECT TO_BASE64(NULL);
Risultato:
+-----------------+ | TO_BASE64(NULL) | +-----------------+ | NULL | +-----------------+
Esempio 5 – Argomento mancante
Riceverai un errore se non passi un argomento:
SELECT TO_BASE64();
Risultato:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'TO_BASE64'
Esempio 6 – Troppi argomenti
Riceverai anche un errore se trasmetti troppi argomenti:
SELECT TO_BASE64('Cat', 'Dog'); Risultato:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'TO_BASE64'