Php 如何将Json关联数组存储到mysql

Php 如何将Json关联数组存储到mysql,php,mysql,json,Php,Mysql,Json,我有来自web服务的JSon数据,并使用 $data = json_decode($postdata, true); using print_r($data); 排列 我想将每四个数组的id、cityName、cityName、cityState、cityPopulation、country插入现有的mysql数据库 您可以将其编码为字符串。看看messagePack()。这是一种非常轻量级的编码 $db = new PDO(...); $query = $db->prepare("

我有来自web服务的JSon数据,并使用

$data = json_decode($postdata, true);

using print_r($data);

排列


我想将每四个数组的id、cityName、cityName、cityState、cityPopulation、country插入现有的mysql数据库

您可以将其编码为字符串。看看messagePack()。这是一种非常轻量级的编码

$db = new PDO(...);
$query = $db->prepare("INSERT INTO mytable "
         ."(id, cityName, cityState, citypopulation,country) "
         ."VALUES (:id, :cityName, :cityState, :cityPopulation, :country)");

foreach ($data as $entry){
    foreach ($entry as $column => $value){
      $query->bindParam(':' . $column, $value);
    }
    $query->execute($entry);
}

或者类似性质的东西…

这是一种很好的在线格式,但对于序列化来说没有那么好,因为它是二进制的,不能用传统的工具进行复制粘贴或操作。我试过了,但没有使用我!“不起作用”不够具体。Well不会在屏幕上显示任何内容@MohammedDizayee:它不应该,它只会默默地将值插入数据库。
$db = new PDO(...);
$query = $db->prepare("INSERT INTO mytable "
         ."(id, cityName, cityState, citypopulation,country) "
         ."VALUES (:id, :cityName, :cityState, :cityPopulation, :country)");

foreach ($data as $entry){
    foreach ($entry as $column => $value){
      $query->bindParam(':' . $column, $value);
    }
    $query->execute($entry);
}