I want to use this mysql select to get local file data.
SELECT
body
FROM posts
WHERE
id in (load_file("/Users/abc/sql/ids"));
What in /Users/abc/sql/ids
:
1,2,3
It didn't cause error but didn't get result.
How to use it in this case?
I want to use this mysql select to get local file data.
SELECT
body
FROM posts
WHERE
id in (load_file("/Users/abc/sql/ids"));
What in /Users/abc/sql/ids
:
1,2,3
It didn't cause error but didn't get result.
How to use it in this case?
This will work (in MySQL 8.0 an above):
set @a = (select cast(load_file('/temp/abc.txt') as char));
select @a;
set @b = concat('select i from integers where i in (',@a,')');
select @b;
prepare st from @b;
execute st;
In this example I do select from a different table, but in the end the SQL statement that you want to execute should be prepared in @b
. (Changing that is easy )
EDIT (about the SQL injection vulnerability):
If the abc.txt
file contains something like this:
1,2;select * from integers;
above will fail with an error ERROR 1243 (HY000): Unknown prepared statement handler (st) given to EXECUTE
This is because select i from integers where i in (1,2;select * from integers;)
is not a valid SQL statement.
So, conclusion, yes there might be a possibility for SQL injection, but it is hard to find a way to do it. This will not make above statemens free from SQL injection!.
SELECT body
FROM posts
WHERE FIND_IN_SET(id, load_file("/Users/abc/sql/ids"));
This may work even with multiline text file. If:
1,2,3
but 1,2,3,
) - if not then last value will be ignored;11,22,33
but ,11,22,33
) - if not then first value will be ignored.