Solved

How to import text file into database

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Attach to file (img) a unique id 8 25
SSRS 2012 r2 - Parm Drop Down has Date/Time 12 32
PHP Syntax Error 4 27
Cpanel file manager 8 23
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
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 …

770 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