Solved

How to import text file into database

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

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.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

691 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