Solved

Populate MySQL table with dissected text file?

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with mod_substitute 18 78
MySQL - Adding an incremented value to a duplicate string 30 53
Centos webserver sending tons of spam 27 40
php non-object 7 29
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

751 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