PDO: Select field from DB which contains a backslash
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have table with this conent
ID|name
----------------
1 |My Shop No. 6
(Don't ask why the six is within the backslashes, I have no idea, I just have to work with it)
I try to select this specifc value from my table.
My query looks like this:
$sql = "SELECT `id` FROM shops WHERE `name` = :name";
$params = array(':name' => "My Shop No. 6");
$this->fetchOne($sql, $params); // the binding happens in my mysql class
This query does not return any data, it's stating that there is no entry.
When I monitor my SQL statements with the Neor Profiler SQL, I can see that the database is receiving this query:
SELECT `id` FROM shops WHERE `name` = 'My Shop No. \6\'
When I use this query directly in PHPMyAdmin, it also does not return any results.
How can I deal with this kind of case?
There is absolutly no option to change the data in the database.
mysql pdo mariadb
|
show 1 more comment
I have table with this conent
ID|name
----------------
1 |My Shop No. 6
(Don't ask why the six is within the backslashes, I have no idea, I just have to work with it)
I try to select this specifc value from my table.
My query looks like this:
$sql = "SELECT `id` FROM shops WHERE `name` = :name";
$params = array(':name' => "My Shop No. 6");
$this->fetchOne($sql, $params); // the binding happens in my mysql class
This query does not return any data, it's stating that there is no entry.
When I monitor my SQL statements with the Neor Profiler SQL, I can see that the database is receiving this query:
SELECT `id` FROM shops WHERE `name` = 'My Shop No. \6\'
When I use this query directly in PHPMyAdmin, it also does not return any results.
How can I deal with this kind of case?
There is absolutly no option to change the data in the database.
mysql pdo mariadb
Backslash has a meaning in PHP strings too. Tryvar_dump($params)to see what I mean. If fact you get it right in SQL and the syntax is the same.
– Álvaro González
Nov 15 '18 at 13:46
Okay, the problem is that I struggled to create an example case. The string value is actually coming from an URL parameter. Do you think that this is still a problem then? I also tried to replace the backslashes with double backslashes before passing them to the query. But that did not help at all.
– Michael Walter
Nov 15 '18 at 13:52
I made a var_dump. The output is the string as it was received via the url
– Michael Walter
Nov 15 '18 at 13:54
String literals are an entirely different thing. Trustvar_dump(). If a single slash in the URL gets doubled atvar_dump($_GET)you have a different problem.
– Álvaro González
Nov 15 '18 at 13:56
It does not get doubled in the var_dump. It's only doubled within theNeor Profiler/ MySQL. Until then, the string has only one backslash
– Michael Walter
Nov 15 '18 at 13:59
|
show 1 more comment
I have table with this conent
ID|name
----------------
1 |My Shop No. 6
(Don't ask why the six is within the backslashes, I have no idea, I just have to work with it)
I try to select this specifc value from my table.
My query looks like this:
$sql = "SELECT `id` FROM shops WHERE `name` = :name";
$params = array(':name' => "My Shop No. 6");
$this->fetchOne($sql, $params); // the binding happens in my mysql class
This query does not return any data, it's stating that there is no entry.
When I monitor my SQL statements with the Neor Profiler SQL, I can see that the database is receiving this query:
SELECT `id` FROM shops WHERE `name` = 'My Shop No. \6\'
When I use this query directly in PHPMyAdmin, it also does not return any results.
How can I deal with this kind of case?
There is absolutly no option to change the data in the database.
mysql pdo mariadb
I have table with this conent
ID|name
----------------
1 |My Shop No. 6
(Don't ask why the six is within the backslashes, I have no idea, I just have to work with it)
I try to select this specifc value from my table.
My query looks like this:
$sql = "SELECT `id` FROM shops WHERE `name` = :name";
$params = array(':name' => "My Shop No. 6");
$this->fetchOne($sql, $params); // the binding happens in my mysql class
This query does not return any data, it's stating that there is no entry.
When I monitor my SQL statements with the Neor Profiler SQL, I can see that the database is receiving this query:
SELECT `id` FROM shops WHERE `name` = 'My Shop No. \6\'
When I use this query directly in PHPMyAdmin, it also does not return any results.
How can I deal with this kind of case?
There is absolutly no option to change the data in the database.
mysql pdo mariadb
mysql pdo mariadb
asked Nov 15 '18 at 13:04
Michael WalterMichael Walter
914616
914616
Backslash has a meaning in PHP strings too. Tryvar_dump($params)to see what I mean. If fact you get it right in SQL and the syntax is the same.
– Álvaro González
Nov 15 '18 at 13:46
Okay, the problem is that I struggled to create an example case. The string value is actually coming from an URL parameter. Do you think that this is still a problem then? I also tried to replace the backslashes with double backslashes before passing them to the query. But that did not help at all.
– Michael Walter
Nov 15 '18 at 13:52
I made a var_dump. The output is the string as it was received via the url
– Michael Walter
Nov 15 '18 at 13:54
String literals are an entirely different thing. Trustvar_dump(). If a single slash in the URL gets doubled atvar_dump($_GET)you have a different problem.
– Álvaro González
Nov 15 '18 at 13:56
It does not get doubled in the var_dump. It's only doubled within theNeor Profiler/ MySQL. Until then, the string has only one backslash
– Michael Walter
Nov 15 '18 at 13:59
|
show 1 more comment
Backslash has a meaning in PHP strings too. Tryvar_dump($params)to see what I mean. If fact you get it right in SQL and the syntax is the same.
– Álvaro González
Nov 15 '18 at 13:46
Okay, the problem is that I struggled to create an example case. The string value is actually coming from an URL parameter. Do you think that this is still a problem then? I also tried to replace the backslashes with double backslashes before passing them to the query. But that did not help at all.
– Michael Walter
Nov 15 '18 at 13:52
I made a var_dump. The output is the string as it was received via the url
– Michael Walter
Nov 15 '18 at 13:54
String literals are an entirely different thing. Trustvar_dump(). If a single slash in the URL gets doubled atvar_dump($_GET)you have a different problem.
– Álvaro González
Nov 15 '18 at 13:56
It does not get doubled in the var_dump. It's only doubled within theNeor Profiler/ MySQL. Until then, the string has only one backslash
– Michael Walter
Nov 15 '18 at 13:59
Backslash has a meaning in PHP strings too. Try
var_dump($params) to see what I mean. If fact you get it right in SQL and the syntax is the same.– Álvaro González
Nov 15 '18 at 13:46
Backslash has a meaning in PHP strings too. Try
var_dump($params) to see what I mean. If fact you get it right in SQL and the syntax is the same.– Álvaro González
Nov 15 '18 at 13:46
Okay, the problem is that I struggled to create an example case. The string value is actually coming from an URL parameter. Do you think that this is still a problem then? I also tried to replace the backslashes with double backslashes before passing them to the query. But that did not help at all.
– Michael Walter
Nov 15 '18 at 13:52
Okay, the problem is that I struggled to create an example case. The string value is actually coming from an URL parameter. Do you think that this is still a problem then? I also tried to replace the backslashes with double backslashes before passing them to the query. But that did not help at all.
– Michael Walter
Nov 15 '18 at 13:52
I made a var_dump. The output is the string as it was received via the url
– Michael Walter
Nov 15 '18 at 13:54
I made a var_dump. The output is the string as it was received via the url
– Michael Walter
Nov 15 '18 at 13:54
String literals are an entirely different thing. Trust
var_dump(). If a single slash in the URL gets doubled at var_dump($_GET) you have a different problem.– Álvaro González
Nov 15 '18 at 13:56
String literals are an entirely different thing. Trust
var_dump(). If a single slash in the URL gets doubled at var_dump($_GET) you have a different problem.– Álvaro González
Nov 15 '18 at 13:56
It does not get doubled in the var_dump. It's only doubled within the
Neor Profiler / MySQL. Until then, the string has only one backslash– Michael Walter
Nov 15 '18 at 13:59
It does not get doubled in the var_dump. It's only doubled within the
Neor Profiler / MySQL. Until then, the string has only one backslash– Michael Walter
Nov 15 '18 at 13:59
|
show 1 more comment
1 Answer
1
active
oldest
votes
Many products (PHP, MySQL, etc) use backslash as the escape character.
By itself, it is usually consumed by doing something to the character after it. For example, atb gives you the letters a and b separated by a tab (the usual interpretation of t).
Doubling up the backslash \xyz usually is intepreted thus: The first backslash escapes the second, so you get xyz.
If your string goes through multiple layers of code (eg, PDO plus MySQL), you might need twice as many backslashes, since each layer will turn \ into .
Another diagnostic tool is SELECT HEX(col) ... to see what was stored in the database. Examples:
610962 atb -- a=61, tab=09, b=62
615C7462 a\tb -- a=61, =5C, t=74, b=62
Back to your code:
$params = array(':name' => "My Shop No. 6");
6 is probably treated as just "6" (not everything has some escaped meaning)
" possibly caused a syntax error. Normally this is now to get a " inside a string
'My Shop No. \6\'
Now the pairs of backslashes are turned into single backslashes, then the string is handed to whoever is next.
(In these contexts, single- and double-quotes work the same.)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320127%2fpdo-select-field-from-db-which-contains-a-backslash%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Many products (PHP, MySQL, etc) use backslash as the escape character.
By itself, it is usually consumed by doing something to the character after it. For example, atb gives you the letters a and b separated by a tab (the usual interpretation of t).
Doubling up the backslash \xyz usually is intepreted thus: The first backslash escapes the second, so you get xyz.
If your string goes through multiple layers of code (eg, PDO plus MySQL), you might need twice as many backslashes, since each layer will turn \ into .
Another diagnostic tool is SELECT HEX(col) ... to see what was stored in the database. Examples:
610962 atb -- a=61, tab=09, b=62
615C7462 a\tb -- a=61, =5C, t=74, b=62
Back to your code:
$params = array(':name' => "My Shop No. 6");
6 is probably treated as just "6" (not everything has some escaped meaning)
" possibly caused a syntax error. Normally this is now to get a " inside a string
'My Shop No. \6\'
Now the pairs of backslashes are turned into single backslashes, then the string is handed to whoever is next.
(In these contexts, single- and double-quotes work the same.)
add a comment |
Many products (PHP, MySQL, etc) use backslash as the escape character.
By itself, it is usually consumed by doing something to the character after it. For example, atb gives you the letters a and b separated by a tab (the usual interpretation of t).
Doubling up the backslash \xyz usually is intepreted thus: The first backslash escapes the second, so you get xyz.
If your string goes through multiple layers of code (eg, PDO plus MySQL), you might need twice as many backslashes, since each layer will turn \ into .
Another diagnostic tool is SELECT HEX(col) ... to see what was stored in the database. Examples:
610962 atb -- a=61, tab=09, b=62
615C7462 a\tb -- a=61, =5C, t=74, b=62
Back to your code:
$params = array(':name' => "My Shop No. 6");
6 is probably treated as just "6" (not everything has some escaped meaning)
" possibly caused a syntax error. Normally this is now to get a " inside a string
'My Shop No. \6\'
Now the pairs of backslashes are turned into single backslashes, then the string is handed to whoever is next.
(In these contexts, single- and double-quotes work the same.)
add a comment |
Many products (PHP, MySQL, etc) use backslash as the escape character.
By itself, it is usually consumed by doing something to the character after it. For example, atb gives you the letters a and b separated by a tab (the usual interpretation of t).
Doubling up the backslash \xyz usually is intepreted thus: The first backslash escapes the second, so you get xyz.
If your string goes through multiple layers of code (eg, PDO plus MySQL), you might need twice as many backslashes, since each layer will turn \ into .
Another diagnostic tool is SELECT HEX(col) ... to see what was stored in the database. Examples:
610962 atb -- a=61, tab=09, b=62
615C7462 a\tb -- a=61, =5C, t=74, b=62
Back to your code:
$params = array(':name' => "My Shop No. 6");
6 is probably treated as just "6" (not everything has some escaped meaning)
" possibly caused a syntax error. Normally this is now to get a " inside a string
'My Shop No. \6\'
Now the pairs of backslashes are turned into single backslashes, then the string is handed to whoever is next.
(In these contexts, single- and double-quotes work the same.)
Many products (PHP, MySQL, etc) use backslash as the escape character.
By itself, it is usually consumed by doing something to the character after it. For example, atb gives you the letters a and b separated by a tab (the usual interpretation of t).
Doubling up the backslash \xyz usually is intepreted thus: The first backslash escapes the second, so you get xyz.
If your string goes through multiple layers of code (eg, PDO plus MySQL), you might need twice as many backslashes, since each layer will turn \ into .
Another diagnostic tool is SELECT HEX(col) ... to see what was stored in the database. Examples:
610962 atb -- a=61, tab=09, b=62
615C7462 a\tb -- a=61, =5C, t=74, b=62
Back to your code:
$params = array(':name' => "My Shop No. 6");
6 is probably treated as just "6" (not everything has some escaped meaning)
" possibly caused a syntax error. Normally this is now to get a " inside a string
'My Shop No. \6\'
Now the pairs of backslashes are turned into single backslashes, then the string is handed to whoever is next.
(In these contexts, single- and double-quotes work the same.)
answered Nov 15 '18 at 21:10
Rick JamesRick James
71k566106
71k566106
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320127%2fpdo-select-field-from-db-which-contains-a-backslash%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Backslash has a meaning in PHP strings too. Try
var_dump($params)to see what I mean. If fact you get it right in SQL and the syntax is the same.– Álvaro González
Nov 15 '18 at 13:46
Okay, the problem is that I struggled to create an example case. The string value is actually coming from an URL parameter. Do you think that this is still a problem then? I also tried to replace the backslashes with double backslashes before passing them to the query. But that did not help at all.
– Michael Walter
Nov 15 '18 at 13:52
I made a var_dump. The output is the string as it was received via the url
– Michael Walter
Nov 15 '18 at 13:54
String literals are an entirely different thing. Trust
var_dump(). If a single slash in the URL gets doubled atvar_dump($_GET)you have a different problem.– Álvaro González
Nov 15 '18 at 13:56
It does not get doubled in the var_dump. It's only doubled within the
Neor Profiler/ MySQL. Until then, the string has only one backslash– Michael Walter
Nov 15 '18 at 13:59