Ashok Gujjar
Ashok Gujjar

Reputation: 441

how to reverse back of mysql_escape_string function in php?

I am storing

[{
    "id": "a34da9b020ec86dc5a7832620082ed5d",
    "type": "img",
    "name": "1458561170282.jpg",
    "mime_type": "image/jpg",
    "path": "http://images.website.com/boxdata/asset22756/images/1458561170282.jpg?r=160321075523",
    "thumb": "http://images.website.com/boxdata/asset22756/images/thumb__1458561170282.jpg?r=160321075523",
    "short_link": "http://website.com/img1084109"
}]

in a MySQL database using mysql_escape_string function.

When I retrieve the data I have the following output:

[\n  {\n    \"short_link\" : \"http:\\\\/\\\\/website.com\\\\/img1085805\",\n    \"id\" : \"ca96c55e1aa76fbca17ee08cad7e5a15\",\n    \"thumb\" : \"http:\\\\/\\\\/images.website.com\\\\/boxdata\\\\/asset22114\\\\/images\\\\/thumb__chat_files_41.jpg?r=160421015318\",\n    \"type\" : \"img\",\n    \"path\" : \"http:\\\\/\\\\/images.website.com\\\\/boxdata\\\\/asset22114\\\\/images\\\\/chat_files_41.jpg?r=160421015318\",\n    \"name\" : \"chat_files_41.jpg\",\n    \"mime_type\" : \"image\\\\/jpg\"\n  }\n]

How can I convert my data back in the initial format?

Upvotes: 0

Views: 840

Answers (4)

RedPoppy
RedPoppy

Reputation: 623

Use PDO:

<?php
/* Execute a prepared statement by passing an array of values */
$sql = 'INSERT INTO mytable (my_json_col) VALUES (:json_string)';
$sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->execute([':json_string' => '[{
  "id": "a34da9b020ec86dc5a7832620082ed5d",
  "type": "img",
  "name": "1458561170282.jpg",
  "mime_type": "image/jpg",
  "path": "http://images.website.com/boxdata/asset22756/images/1458561170282.jpg?r=160321075523",
  "thumb": "http://images.website.com/boxdata/asset22756/images/thumb__1458561170282.jpg?r=160321075523",
  "short_link": "http://website.com/img1084109"}]']);
// retrieve with fetch*
$sql = 'SELECT * FROM mytable WHERE id=:my_id';
$sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->execute([':my_id' => 1]); //my_id is the DB row id you want
$sth->fetchObject(); //this will give you a stdClass object with row names as properties
?>

Upvotes: 0

dhruv jadia
dhruv jadia

Reputation: 1680

Hope this helps

$str='[\n {\n "short_link" : "http:\/\/website.com\/img1085805",
\n "id" : "ca96c55e1aa76fbca17ee08cad7e5a15",
\n "thumb" : "http:\/\/images.website.com\/boxdata\/asset22114\/images\/thumb__chat_files_41.jpg?r=160421015318",
\n "type" : "img",\n "path" : "http:\/\/images.website.com\/boxdata\/asset22114\/images\/chat_files_41.jpg?r=160421015318",
\n "name" : "chat_files_41.jpg",\n "mime_type" : "image\/jpg"\n }\n]';
echo stripslashes(str_replace('\n','',$str));

check screenshot enter image description here

Upvotes: 1

Basile Perrenoud
Basile Perrenoud

Reputation: 4110

Try this:

$correctString = nl2br(stripslashes($result));

By the way, you should use

mysqli_real_escape_string ($connection ,$source )  

instead of mysql_escape_string

Upvotes: 0

Harikrishnan
Harikrishnan

Reputation: 9979

Use stripslashes function to remove slashes.

$string = stripslashes($escaped_string);

Upvotes: 0

Related Questions