If you properly escape the string you are trying to insert, you will not have the problem with the \
disappearing. It is actually MySQL that is treating the single backslash as an escape string.
So something like this:
$path_string_escaped = mysqli_real_escape_string($db_connection, $path_string);
And use $path_string_escaped
in your insert.
Note that this with make $path_string_escaped
look something like C:\\users\\files\\test.php
. MySQL will then unescape the backslashes such that C:\users\files\test.php
will be written to database. You will not need to do anything when reading from the database to modify the string.
You should ALWAYS escape user-provided or user-accessible data you will be writing to the database.
This is also a good example of why, even if using prepared statements, that you should still escape user-accessible data you are writing to DB when you are dealings with string that could contain MySQL escape sequences. While using prepared statements can certainly prevent against an actual injection attack, it will not help you actually write your data to the database the way you intend when there are already MySQL escape sequences in the strings.