Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to import text file into database

Posted on 2013-01-29
2
Medium Priority
?
327 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

636 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