How to add SQL file with PHP to WordPress database

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

Related posts

Leave a Reply

2 comments

  1. 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>';
                        }
                        ?>