Solved

How to import text file into database

Posted on 2013-01-29
2
311 Views
Last Modified: 2013-01-29
I have a text file that contains a music genre on each line, as follows:

Adult Contemporary 
Acapella 
Acid Jazz 
Acoustic 
Acoustic Alternative 
Adult Alternative 
African Sound 
Aggro Rock 
Alt-Country 
AlternativeAmbient                             
AmericanaBig Band                             
Bluegrass                             
Blues                             
Blues/R&B                            
BluesRock 
Books                            
Brass                            
Cabaret                            
Cajun                            
Cello                            
Celtic                            
Chamber                            
Chant                            
Childrens                            
Christian                            
Christian Alternative                            
Christian Contemporary                            
Christian Country                            
Christian Rock                             
Christmas  

Open in new window


( there are lots more )

I need to import these genres into my database, which is simply structured as follows:

GENRES TABLE:

id  int  auto increment
genre_name  varchar 255

How can insert all the genres from the txt file using either a MySQL query, or using a php script?

I certainly don't want to have to do this manually using PhpMyAdmin, ya know!

Thanks!
0
Comment
Question by:edvinson
  • 2
2 Comments
 
LVL 1

Accepted Solution

by:
edvinson earned 0 total points
ID: 38833458
I found my own solution ( not sure how to go about assigning points )

<?php
   
    $conn = mysql_connect('localhost', 'root', '') or die ('Error connecting to mysql');
    mysql_select_db('plugasong', $conn) or die ('Error connecting to database');
    $query = 'insert into genres (name) values ("%s");';
     
    $lines = file('genres.txt');//your filename
    for($i=0; $i < count($lines); $i++){
    mysql_query(sprintf($query, mysql_real_escape_string($lines[$i]))) or die(mysql_error() . '<br><br>' . '<b>Query:</b> ' . $query);
    }
   

?>
0
 
LVL 1

Author Closing Comment

by:edvinson
ID: 38833462
I found own solution
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now