一位被互联网耽误的文艺青年

有人说,生命是晚霞衬红日,青松立峭壁,万里黄沙落秋雁,三月阳春映白雪,是金刀宝马,是银貂美酒,是皓月清歌的漂泊……

将JSON文件导入MYSQL数据库,三级联动省市县

将JSON文件导入MYSQL数据库,三级联动省市县

<?php
header(“Content-Type: text/html; charset=utf-8”);
date_default_timezone_set(‘PRC’);
error_reporting(0);
ob_start();
session_start(); //打开会话
//session_set_cookie_params( 12*60*60 );
//session_cache_expire(60*60);
$dblink=@mysql_connect(“111.111.111.111″,”username”,”password”); //mysql主机,用户名,密码
if($dblink==null)
{
echo “数据库打开失败”;
exit;
} //如果数据库无法链接则提示
mysql_select_db(“database”); //选择数据库
mysql_query(“SET NAMES utf8”); //mysql 字符集 UTF-8
// 从文件中读取数据到PHP变量

//https://github.com/modood/Administrative-divisions-of-China   下载
$json_string = file_get_contents(‘citys.json’);

// 用参数true把JSON字符串强制转成PHP数组
$data = json_decode($json_string, true);

foreach ($data as $key => $value) {
$code1=$value[“code”];
$name1=$value[“name”];
$sql1=”insert into t_dq1 (code,name) values (‘$code1′,’$name1’)”;
mysql_query($sql1);

foreach ($value[“children”] as $k => $v) {

$code2=$v[“code”];
$name2=$v[“name”];
$sql2=”insert into t_dq2 (code,name,fsort) values (‘$code2′,’$name2′,’$code1’)”;
mysql_query($sql2);

foreach ($v[“children”] as $k3 => $v3) {

$code3=$v3[“code”];
$name3=$v3[“name”];

$sql3=”insert into t_dq3 (code,name,fsort) values (‘$code3′,’$name3′,’$code2’)”;
mysql_query($sql3);
}
}

}
?>

oldmyth