Solved

How to import text file into database

Posted on 2013-01-29
2
315 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
I think I know what this error means, but I need some other eyes on it... 32 49
mysql query for sum() 3 28
IP 10.0.1.2 / 255.0.0.0 61 56
RAISERROR WITH NOWAIT 2 16
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

856 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