Solved

Populate MySQL table with dissected text file?

Posted on 2006-07-03
1
243 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

867 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

15 Experts available now in Live!

Get 1:1 Help Now