简体   繁体   中英

Maintain forward slash from MySQL select in PHP

DIRECT FROM DB

When I select directly from db I get the forward slash without problem.

Query:

SELECT REPLACE(`row`, '"', '\\"') as data FROM `table`

Direct query result:

a:1:{i:0;s:0:\"\";}

FROM PHP

But when done in PHP (specifically CI), forward slash are gone. Query string:

"SELECT REPLACE(`row`, '\"', '\\\"') as data FROM `table`"

Query from PHP result:

a:1:{i:0;s:0:"";}

Question: How do I maintain the forward slash in PHP?


Note: Im trying to have a group_concat with other values as json format string so I need the slashes so it doesn't break the format, sample complete output:

{"field_1":"Some Value","field_2":"a:1:{i:0;s:0:\"\";}"}

Your two queries are not equivalent: your PHP code evaluates to

php > echo "SELECT REPLACE(`row`, '\"', '\\\"') as data FROM `table`";
SELECT REPLACE(`row`, '"', '\"') as data FROM `table`

You're only escaping a single slash; you need an extra pair:

"SELECT REPLACE(`row`, '\"', '\\\\\"') as data FROM `table`"

which results in your original query:

php > echo "SELECT REPLACE(`row`, '\"', '\\\\\"') as data FROM `table`";
SELECT REPLACE(`row`, '"', '\\"') as data FROM `table`

I dont get what goal is this, but maybe you can use this bro. the same result as forwards slashes.

json_encode(); 

When select, just return json_encode($value) so that it will maintain the forward slashes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM