Solved

Populate MySQL table with dissected text file?

Posted on 2006-07-03
1
252 Views
Last Modified: 2012-06-27
I am trying to devise a script that will open and read a text file and create a separate MySQL row for content separated by two carriage returns

For example the following text file would create three MySQL rows:

       hello
       my name is fred

       more random stuff

       even more
       bananas and whatnot

The source text file contains some 18k words.

Is this possible?
0
Comment
Question by:SandyCooke
1 Comment
 
LVL 35

Accepted Solution

by:
Raynard7 earned 500 total points
ID: 17034593
Try the below code - this reads the file ($filename) - breakes it up into an array - a new array item wherever there are two carraige returns and then creates an insert statement - you will need to tailor this

This should not be a problem with a file of this size

<? php
$content = fread( $fp, filesize( $filename ) );
$pieces = explode("\n\n", $content);
foreach($pieces as $val)
   $sql = "insert into tableName (columnName) values (" . mysql_escape_string($val) ");";
   // Run the instert statement - however you have mysql / mysqli connected
} // end foreach
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

831 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