<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Online PHP Script Execution</title>
    </head>
    <body>
        <?php
            $handle = fopen("http://data.kaohsiung.gov.tw/Opendata/DownLoad.aspx?Type=2&CaseNo1=AV&CaseNo2=1&FileType=1&Lang=C&FolderType=","rb");
            $content = "";
            while (!feof($handle)) {
                $content .= fread($handle, 10000);
            }
            fclose($handle);

            $servername = "localhost";
            $username = "root";
            $password = "root";
            $dbname = "KHtravel";

            $conn = new mysqli($servername, $username, $password, $dbname);
            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            }

            $content = json_decode($content);
            foreach ($content as $key => $value) {
                $sql = "INSERT INTO travelstation (id, name, toldescribe, tel, address, picture1, px, py, opentime, changetime) VALUES ('$value->Id', '$value->Name', '$value->Toldescribe', '$value->Tel', '$value->Add', '$value->Picture1', '$value->Px', '$value->Py', '$value->Opentime', '$value->Changetime')";
                if (mysqli_query($conn, $sql)) {
                        echo "New record created successfully";
                } else {
                        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
                }

            }
            mysqli_close($conn);
        ?>
    </body>
</html>

arrow
arrow
    文章標籤
    php read json import to mysql
    全站熱搜
    創作者介紹
    創作者 痞客興 的頭像
    痞客興

    痞客興的部落格

    痞客興 發表在 痞客邦 留言(1) 人氣()