City and State MySQL Database For You

Discussion in 'General Marketing' started by chatmasta, Apr 11, 2007.

  1. #1
    Hey,

    There are many uses for a database of all US cities. I have here a database with every city, what state it's in, and that state's abbreviation. There are some 6,000 rows in it. You will need MySQL and PHP. Read below for more.

    STEP 1
    --------------------

    Create a database called "city_state" in phpMyAdmin or whatever. Then, also in phpMyAdmin, run this MySQL query.

    -- phpMyAdmin SQL Dump
    -- version 2.9.2
    -- http://www.phpmyadmin.net
    -- 
    -- Host: localhost
    -- Generation Time: Apr 11, 2007 at 10:14 PM
    -- Server version: 5.0.27
    -- PHP Version: 5.2.1
    -- 
    -- Database: `city_state`
    -- 
    
    -- --------------------------------------------------------
    
    -- 
    -- Table structure for table `cities`
    -- 
    
    CREATE TABLE `cities` (
      `id` int(11) NOT NULL auto_increment,
      `state_id` int(11) NOT NULL,
      `city` varchar(255) NOT NULL,
      PRIMARY KEY  (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5998 ;
    
    -- --------------------------------------------------------
    
    -- 
    -- Table structure for table `states`
    -- 
    
    CREATE TABLE `states` (
      `id` int(11) NOT NULL auto_increment,
      `state` varchar(255) NOT NULL,
      `abb` varchar(2) NOT NULL,
      PRIMARY KEY  (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=52 ;
    
    
    Code (markup):
    STEP 2
    --------------------

    Download attached zip file. Upload all the files to the server with your MySQL database on it (I'm using my localhost).

    STEP 4
    --------------------

    Open up cities/pop.php and at the top, set $dbhost, $dbuser, and $dbpass to be whatever your values are. Most likely $dbhost will be localhost.

    STEP 5
    --------------------

    Run pop.php. Most likely this is http://www.yourdomain.com/cities/pop.php. Be patient, it will take a while as it needs to add around 6000 rows to the database.

    Caution: this WILL slow down your server while it populates the database.




    So there you have it. You now have a MySQL database with every city name in the US. There are a lot of uses for this, and it's all up to your imagination. The beauty is that you can manipulate the data in almost any way imaginable using queries and some clever PHP.

    If you have any questions, just ask.
     

    Attached Files:

    chatmasta, Apr 11, 2007 IP
  2. chuckd1356

    chuckd1356 Active Member

    Messages:
    770
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    70
    #2
    This is great man. It's just what I need. Now I can do so much more with marketing.

    I can actually target my users better.

    Thanks! Rep!
     
    chuckd1356, Apr 11, 2007 IP
  3. bunnybling

    bunnybling Peon

    Messages:
    292
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Absolutely awsome! Thanks!
     
    bunnybling, Apr 11, 2007 IP
  4. quaffapint

    quaffapint Active Member

    Messages:
    299
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Thanks for this - appreciate you writing it up and sharing...Green for you!
     
    quaffapint, Apr 11, 2007 IP
  5. chatmasta

    chatmasta Peon

    Messages:
    693
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #5
    UPDATE!

    I forgot to add state abbreviations. If you've already downloaded it, just go into phpMyAdmin and run the following on the city_state database:

    ALTER TABLE `states` CHANGE `abb` `abb` VARCHAR( 2 ) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL 
    Code (markup):
    Then download the attached file, edit it to put in host/user/pass, and run it.

    Otherwise, I updated the first post and it will work fine.
     

    Attached Files:

    chatmasta, Apr 11, 2007 IP
  6. davidm19401

    davidm19401 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I get an invalid attachment message when I try to download cities.zip
     
    davidm19401, Apr 11, 2007 IP
  7. chatmasta

    chatmasta Peon

    Messages:
    693
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You probably downloaded it right when I was updating the first post. Try again.
     
    chatmasta, Apr 11, 2007 IP
  8. davidm19401

    davidm19401 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Looks like that was the problem. It download fine now. Thanks!
     
    davidm19401, Apr 11, 2007 IP
  9. chuckd1356

    chuckd1356 Active Member

    Messages:
    770
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    70
    #9
    Thanks, even better now!
     
    chuckd1356, Apr 12, 2007 IP
  10. jbladeus

    jbladeus Peon

    Messages:
    485
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #10
    fantastic post.
    Exactly what i was looking for.
    Reps.
     
    jbladeus, Apr 21, 2007 IP
  11. chatmasta

    chatmasta Peon

    Messages:
    693
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #11
    chatmasta, Apr 21, 2007 IP
  12. lukemcd

    lukemcd Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    This is a great resource. I would like to point out I had trouble running the script on my server because the filenames for the states are lowercase (except "District of Columbia.txt") but the script looks for them with first letter capitalized.

    You can fix it by changing the filenames (e.g. "alabama.txt" to "Alabama.txt") or changing the line in pop.php:
    $fn = 'list/' . $state . '.txt';
    to:
    $fn = 'list/' . strtolower($state) . '.txt';

    Not that if you do this you do need to change "District of Columbia.txt" to "district of columbia.txt"
     
    lukemcd, Apr 24, 2007 IP
  13. chatmasta

    chatmasta Peon

    Messages:
    693
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #13
    chatmasta, Apr 24, 2007 IP