This function helps in reading a file and returns its contents as a string.
Some most important part of the function
Lets see the below code to get the value of variable.
mysql> SHOW VARIABLES LIKE 'max_allowed_packet'; +--------------------+----------+ | Variable_name | Value | +--------------------+----------+ | max_allowed_packet | 16777216 | +--------------------+----------+ 1 row in set (0.08 sec)
This is the most important part of the function. Otherwise you will waste your time and get the NULL error.
Lets see the below code to get the value of variable.
mysql> SHOW VARIABLES LIKE 'secure_file_priv'; +------------------+-----------------------+ | Variable_name | Value | +------------------+-----------------------+ | secure_file_priv | /var/lib/mysql-files/ | +------------------+-----------------------+ 1 row in set (0.00 sec)
If the file does not exist or cannot be read/found because one of the preceding conditions is not satisfied, the function returns NULL.
Lets see the below code to get the value of variable.
mysql> SHOW VARIABLES LIKE 'character_set_filesystem'; +--------------------------+--------+ | Variable_name | Value | +--------------------------+--------+ | character_set_filesystem | binary | +--------------------------+--------+ 1 row in set (0.76 sec)
[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|lef|lef|left”]
Name, Required /Optional, Value Type, Description
string , Required, String , The filename.
[/table]
[table caption=”” width=”100%” colwidth=”20%|80%” colalign=”left|left”]
Returns,
string, returns file contents as a string.
[/table]
[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
Version, MySQL 5.7
[/table]
Lets see the below example, where I select the contents from a file:
mysql> SELECT LOAD_FILE('/var/lib/mysql-files/test.txt') AS Output; +---------------------+ | Output | +---------------------+ this is test file. +---------------------+ 1 row in set (0.09 sec)
Lets see the below example of a query might look like when inserting the contents of the file into a table:
mysql> INSERT INTO tbl_test (id, user_name, cv_text) VALUES (1, 'emp', LOAD_FILE('/var/lib/mysql-files/test.txt'));
And now we can fetch the row of a table:
mysql> SELECT cv_text FROM tbl_test WHERE id = 1; +---------------------+ | cv_text | +---------------------+ this is test file. +---------------------+ 1 row in set (0.04 sec)
In this case, the column cv_text has a data type of BLOB (which allows it to store binary data).
NULL is returned, if the file doesn’t exist. Lets see the below example.
mysql> SELECT LOAD_FILE('/var/lib/mysql-files/test1.txt') AS Output; +--------+ | Output | +--------+ | NULL | +--------+ 1 row in set (1.41 sec)
See all MySQL String functions MySQL 8 String Functions.
When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…
In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…
In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…
In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…
When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…
In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…