如何在php中使用一个查询将700条记录插入到mysql数据库中

时间:2015-08-08 03:06:25

标签: php mysql wordpress

我已经尝试了几天,我在一个mysql数据库中有700多条记录,我使用select查询得到然后做一些事情。但是当我想重新插入数据时,插入需要很长时间。我怎样才能加快提升任何帮助

       $zip_db_name = $wpdb->prefix . 'wa_zip_codes';

        $taxTable = $wpdb->prefix . "woocommerce_tax_rates";

        $taxTableLocation = $wpdb->prefix . "woocommerce_tax_rate_locations";   

        $getzip = $wpdb->get_results("SELECT zip FROM $zip_db_name ORDER BY zip DESC");

        foreach ($getzip as $retrieved_data){
            $zipcode = $retrieved_data->zip;                

            $url = 'http://dor.wa.gov/AddressRates.aspx?output=xml&addr=&city=&zip='. $zipcode .'';
            $xml = simplexml_load_file($url) or die("feed not loading");

            foreach ($xml as $value){
                //data to be stored
                $country = 'US';

                $state = 'WA';                      

                $city = '*';

                $rate = $xml['rate'];   

                $wpdb->query("START TRANSACTION");          

                    // Inserts values into woocommerce_tax_rates                                                    
                    $wpdb->query("INSERT INTO $taxTable (tax_rate_country, tax_rate_state, tax_rate, tax_rate_name) VALUES ('$country', '$state', '$rate', '$city')");              

                    $thelastId = $wpdb->get_results("SELECT tax_rate_id FROM $taxTable ORDER BY tax_rate_id DESC LIMIT 1;");

                    //Gets the ID of the last inserted value in wpdb

                    $lastid = $wpdb->insert_id;

                    //Inserts zip code value into wp_woocommerce_tax_rate_locations

                    $wpdb->query("INSERT INTO $taxTableLocation (tax_rate_id, location_code, location_type) VALUES ('$lastid', '$zipcode', 'postcode')");                       

                $wpdb->query("COMMIT");
            }   
        }

0 个答案:

没有答案