Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Populate MySQL table with dissected text file?

Posted on 2006-07-03
1
Medium Priority
?
298 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
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.
Suggested Courses

886 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