Rumah > php教程 > PHP源码 > teks badan

随机像mariadb插入10万数据,包括经纬度以及调用百度map API获取省份

PHP中文网
Lepaskan: 2016-05-25 17:01:48
asal
1420 orang telah melayarinya

在做的一个项目需要统计一些数据,这里模拟插入10000数据。

其中数据包括经纬度(app上传上来的),这里要调用百度API获取这个经纬度的身份存起来,供后面调用echarts画全国地图各省份分布的.

这里随机的生成经纬度跟时间,

有些细节处理的不是很好啦,无所谓了,随机插入数据即可。

<?php
    $link=mysqli_connect("localhost","root","dengli","yilonggu");
    $link->query("delete from message");
    function suijishijian()
    {
        $month=array(&#39;0&#39;,&#39;01&#39;,&#39;02&#39;,&#39;03&#39;,&#39;04&#39;,&#39;05&#39;,&#39;06&#39;,&#39;07&#39;,&#39;08&#39;,&#39;09&#39;,&#39;10&#39;,&#39;11&#39;,&#39;12&#39;);
        $days=array(&#39;0&#39;,&#39;01&#39;,&#39;02&#39;,&#39;03&#39;,&#39;04&#39;,&#39;05&#39;,&#39;06&#39;,&#39;07&#39;,&#39;08&#39;,&#39;09&#39;,&#39;10&#39;,&#39;11&#39;,&#39;12&#39;,&#39;13&#39;,&#39;14&#39;,&#39;15&#39;,&#39;16&#39;,&#39;17&#39;,&#39;18&#39;,&#39;19&#39;,&#39;20&#39;,&#39;21&#39;,&#39;22&#39;,&#39;23&#39;,&#39;24&#39;,&#39;25&#39;,&#39;26&#39;,&#39;27&#39;,&#39;28&#39;,&#39;29&#39;,&#39;30&#39;,&#39;31&#39;);
        $hour=array(&#39;0&#39;,&#39;01&#39;,&#39;02&#39;,&#39;03&#39;,&#39;04&#39;,&#39;05&#39;,&#39;06&#39;,&#39;07&#39;,&#39;08&#39;,&#39;09&#39;,&#39;10&#39;,&#39;11&#39;,&#39;12&#39;,&#39;13&#39;,&#39;14&#39;,&#39;15&#39;,&#39;16&#39;,&#39;17&#39;,&#39;18&#39;,&#39;19&#39;,&#39;20&#39;,&#39;21&#39;,&#39;22&#39;,&#39;23&#39;,&#39;00&#39;);
        $fz=array("","00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59");
        $day=array(
                 "01"=>"31",
                 "02"=>"28",
                 "03"=>"31",
                 "04"=>"30",
                 "05"=>"31",
                 "06"=>"30",
                 "07"=>"31",
                 "08"=>"31",
                 "09"=>"30",
                 "10"=>"31",
                 "11"=>"30",
                 "12"=>"31");

        $y=rand(2010,2013);
        $m=$month[rand(1,12)];
        $d=$days[rand(1,$day["$m"])];
        $h=$hour[rand(1,24)];
        $mm=$fz[rand(1,60)];
        $s=$fz[rand(1,60)];
        return "$y-$m-$d $h:$mm:$s\\n";

    }

    function floadNumber($min,$max)
    {
         return $min + mt_rand() / mt_getrandmax() * ($max - $min);  
    }

    function shengfen($jd,$wd)
    {
        $url="http://api.map.baidu.com/geocoder?location={$wd},{$jd}&output=json";
        $json=json_decode(file_get_contents($url),TRUE);
        return $json[&#39;result&#39;][&#39;addressComponent&#39;][&#39;province&#39;];
    }
    for($i=1;$i<10000;$i++)
    {
        $zid=rand(1,99999);
        $time=suijishijian();
        $jingdu=floadNumber(100,120);
        $weidu=floadNumber(30,40);
        $province=shengfen($jingdu,$weidu);
        $lid=rand(1,13);
        $type=rand(0,3);
        $tag=rand(1,4);
        $sbh=md5(time());
        $sql="insert into `tongji` (zid,time,jingdu,weidu,province,lid,type,tag,sbh) VALUES (&#39;{$zid}&#39;,&#39;{$time}&#39;,&#39;{$jingdu}&#39;,&#39;{$weidu}&#39;,&#39;{$province}&#39;,&#39;{$lid}&#39;,&#39;{$type}&#39;,&#39;{$tag}&#39;,&#39;{$sbh}&#39;)";
        $link->query($sql);  
        echo $sql."\\n";
    }
    $link->close();?>
Salin selepas log masuk
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan