【问题标题】:Primary key ID field auto increment not allowing JSON import. PHP/PDO and MySQL主键 ID 字段自动递增不允许 JSON 导入。 PHP/PDO 和 MySQL
【发布时间】:2016-08-13 04:43:04
【问题描述】:

我正在尝试使用 PHP/PDO 将 JSON 文件导入 MySQL 数据库。如果没有自动递增的 ID 字段,它将很好地导入,但是当我添加该字段时,JSON 文件根本不会导入。

表....

Id 字段设置为主键,自动递增。

示例 JSON 数据...

{
    "name": "Google", 
    "uri": "https://www.google.com", 
    "description": "The largest and most popular search engine in the world",
}, 
{
    "name": "NFL", 
    "uri": "https://www.nfl.com", 
    "description": "The National Football League",
}, 
{
    "name": "CNN", 
    "uri": "https://www.cnn.com", 
    "description": "Cable News Network",
},

这是我用来导入字段的 PHP/PDO 代码...

$jsondata = file_get_contents('http://www.somewebsite/?format=json');
$data = json_decode($jsondata, true);

$stmt = $db->prepare("insert into bookmark values(?,?,?)");

foreach ($data as $row) {
    $stmt->bindParam(1, $row['name']);
    $stmt->bindParam(2, $row['uri']);
    $stmt->bindParam(3, $row['description']);
    $stmt->execute();
}

【问题讨论】:

    标签: php mysql json pdo


    【解决方案1】:

    我认为你应该改变:

    $stmt = $db->prepare("insert into bookmark values(?,?,?)");
    

    $stmt = $db->prepare("insert (name,uri,description) bookmark values(?,?,?)");
    

    $stmt =  $db->prepare("insert bookmark values(null,?,?,?)");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-26
      • 2010-12-09
      • 1970-01-01
      • 1970-01-01
      • 2020-07-18
      • 1970-01-01
      • 2020-08-28
      相关资源
      最近更新 更多