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.
App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…
As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…
Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…
COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…
Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…
For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…