I need to store relative path of a document into a MySQL table. The problem is that when I insert a string of this form:
$urlPath ='\abc\def\fg.jpg'
into the relevant column, what I get is, I have all the slash '\' strip off, with an unknown symbol in front of all the text. So the result in the MySQL table is something like this:
'(weird symbol)abcdeffg.jpg'
As you can see, this kind of data is useless. Any idea how to fix this?
Edit: I tried $urlPath ="\\abc\\def\\fg.jpg" and $urlPath ='\\abc\\def\\fg.jpg', still I got gibberish..
You need to escape it twice, first for PHP because the \ character has a special meaning:
From the docs:
Then you need to escape it again for MySQL because the literal string now contains ‘s which also has special meaning to MySQL.
If you don’t want to depend on MySQL, you can use addslashes instead: