【问题标题】:Php Cant insert Json data with apostrophe to databasePhp无法将带有撇号的Json数据插入数据库
【发布时间】:2016-12-13 00:08:06
【问题描述】:

我有一些从外部 url 检索的 json 数据。我正在存储数据,但似乎跳过了带有撇号“Tes't2”的值。我见过有人说要引用这个,但我不知道该怎么做。我是个菜鸟。谢谢!

这是我的 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);       
 }  

这里是data.json

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

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

]

【问题讨论】:

标签: php mysql json mysqli


【解决方案1】:

试试这个:

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

【讨论】:

    【解决方案2】:

    使用mysqli_real_escape_string($con, $YOUR_VARIABLE); 应该可以工作。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-25
      • 1970-01-01
      • 2011-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多