Solved

Populate MySQL table with dissected text file?

Posted on 2006-07-03
1
229 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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 …

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now