Php Cant将带有撇号的Json数据插入数据库

时间:2021-01-18 15:43:13

I have some json data that i am retrieving from an external url. I am storing the data but It seems that the values that have an apostrophe "Tes't2" is being skipped over. I have seen people say to quote escape this, but i am not sure how to do this. I'm a noobie. Thanks!

我有一些json数据,我从外部URL检索。我正在存储数据,但似乎跳过了包含撇号“Tes't2”的值。我看到有人说引用逃避这个,但我不知道该怎么做。我是个noobie。谢谢!

Here is my php

这是我的PHP

$filename = "http://www.someurl.com/data.json";  
$data = file_get_contents($filename);  
$array = json_decode($data, true);

 foreach($array as $row)  
 {  
      $sql = "INSERT INTO table_popular_items(rank, name) VALUES (
      '".$row["rank"]."', 
      '".$row["name"]."'
      )";       
      mysqli_query($connect, $sql);       
 }  

Here is the data.json

这是data.json

[
    {
        "rank": 1,
        "name": "Test1"
    },
    {
        "rank": 2,
        "name": "Tes't2"

    },
    {
        "rank": 3,
        "name": "Test3"
    }

]

2 个解决方案

#1


1  

Try this:

 $sql = "INSERT INTO `table_popular_items`(`rank`, `name`) VALUES (
  '".$row["rank"]."', 
  '".addslashes($row["name"])."'
  )";

#2


0  

use mysqli_real_escape_string($con, $YOUR_VARIABLE); should work.

使用mysqli_real_escape_string($ con,$ YOUR_VARIABLE);应该管用。

Doc: http://php.net/manual/en/mysqli.real-escape-string.php

#1


1  

Try this:

 $sql = "INSERT INTO `table_popular_items`(`rank`, `name`) VALUES (
  '".$row["rank"]."', 
  '".addslashes($row["name"])."'
  )";

#2


0  

use mysqli_real_escape_string($con, $YOUR_VARIABLE); should work.

使用mysqli_real_escape_string($ con,$ YOUR_VARIABLE);应该管用。

Doc: http://php.net/manual/en/mysqli.real-escape-string.php