Cant insert string to database because of single quote

Viewed 649

I am trying to insert this json's content to my database.

But I get null result on Summary because the text is like that:

summary":"One of the oldest cities in the United States, Boston definitely has an \"old city\" feel to it. Home to great architecture and historical buildings, the city is also a great place to visit with younger ones, as it also plays host to LEGOLAND and many more children's attractions."

I think that this is due to the single quote and my PHP query '{$summary}'.

$url = file_get_contents("urlcontent/api.json");
$arr = json_decode($url, true);

for($i=0;$i < count($arr);$i++){

$id         = $arr[$i]['id'];  
$location   = $arr[$i]['location'];
$summary    = $arr[$i]['summary'];   

$query = "INSERT INTO [databasename].[dbo].[table]( id , summary, location)";
$query .= "VALUES ('{$id}', '{$summary}', '{$location}' )";

$update_query = sqlsrv_query($con, $query);

  if(!$update_query){
      die("There was an error" .print_r( sqlsrv_errors($con), true));
                }
}

I tried to fix that with:

$summary = preg_replace("'", "", $arr[$i]['summary']);
$summary = str_replace("'", "", $arr[$i]['summary']);
REPLACE('{$summary}','''','''') //MSSQL Database command

But still can't insert that text and get NULL result. I can echo it and var_dump and text is OK just cant insert it to my table.

My summary is as text in my database.

Whats the way to make it work? Cheers

1 Answers
Related