Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have SQL file with US zip codes (over 41000 lines of code). I want make a plugin that inserts this SQL file into WP database so user can do a search by zip codes. I want to insert SQL with PHP rather than PhpMyAdmin

share|improve this question

closed as off topic by toscho Jul 5 '12 at 21:26

Questions on WordPress Development Stack Exchange are expected to relate to WordPress within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
did u get solution for this ? Importing .sql file on plugin active ? –  Ajay Patel Jun 26 '12 at 7:13

2 Answers 2

I have not created a plugin that does what you want, but I have been playing with a plugin that creates its own tables. The plugin in comes with an SQL.txt file in case your WordPress install does not have permissions to create tables (in which case you would have to manually run the SQL file)

So it seems that a plugin that creates tables could potentially be problematical during install.

Another thing that this same plugin does is use an array from another file to display things. This seems like it would be a better approach because you would not have to mess with people's DB, and you could easily update the file that holds your array of zip codes.

Here is some of the code it uses for doing that:

 //Build new blog_type array item.
    $blog_type_item = array('name' => $name,
                            'nicename' => empty($nice_name) ? strtolower(str_replace(' ', '_', $name)) : $nice_name,
                            'description' => $description,
                            'signup_only' => $signup_only);

    //Append new blog_type array item to global array.
    $blog_types[] = $blog_type_item;

    return $blog_type_item;



<?php
                    foreach ($blog_types as $blog_type) {
                        $selected = '';
                        foreach ($selected_blog_types as $selected_blog_type) {
                            if ($selected_blog_type == $blog_type['nicename']){
                                $selected = ' selected="selected"';
                                $selected_blog_type_store = $blog_type['nicename'];
                            }
                        }
                        echo '<option value="' . $blog_type['nicename'] . '" ' . $selected . '> ' . $blog_type['name'] . '</option>';
                    }
                    ?>
share|improve this answer

See the WordPress Codex on Creating Tables with Plugins.

share|improve this answer
    
I've seen this, but there is nothing about importing SQL file. Codex just explains how to insert data manually $wpdb->insert –  alexndm Jun 24 '11 at 22:50
2  
Import into what exactly- your own table, a WordPress table? This is more PHP general- how do you import an SQL file via PHP, the answer to that is potentially complicated depending on your server setup- maybe LOAD DATA INFILE? –  Milo Jun 24 '11 at 22:58
    
$newziptable = "CREATE TABLE IF NOT EXISTS " . $table_name . " ( Id int(11) NOT NULL auto_increment, ZipCode varchar(15) , State varchar(10) , City varchar(50) , County varchar(50) , Longitude decimal(10,2) DEFAULT '0.00' , Latitude decimal(10,2) DEFAULT '0.00' , PRIMARY KEY (Id) );"; After I've created new table I want to load all zip codes from my SQL file or just import it entirely –  alexndm Jun 24 '11 at 23:08
1  
Yes definatly a php question rather than wordpress, have you looked at file_get_contents and LOAD DATA INFILE? –  MartinJJ Jun 25 '11 at 7:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.