Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Submit Textfile Contents To Database
#6
(10-09-2010, 09:29 PM)Disease Wrote: You aren't going to be assigning each line as its own variable. You'd need to know exactly how many lines there are in the file and then it wouldn't be extensible nor flexible. Rather you can use an array. Simply put, an array is a container of values. PHP offers a very simple way to feed each line of a file directory into an array via its file () function.

From there you'd want to build a SQL INSERT statement. Supposing you're using MySQL, you can insert multiple rows in one INSERT statement by creating a list of VALUE pairs. Example:

Code:
INSERT into table_name (col1, col2) VALUES (someData, someData), (moreData, moreData), (evenMoreData, evenMoreData)

An example:

PHP Code:
<?php

mysql_connect 
("server""username""password");
mysql_select_db ("database_name");

// Build the lines array, thus $lines[0] will be the first and etc.
$lines array_map ('trim'file ("file"FILE_SKIP_EMPTY_LINES));
$sql "INSERT INTO table_name (column_name) VALUES ";

foreach (
$lines as $index => $curLine)
{
  
$sql .= "('$curLine')";
  
  
// count () returns the number of lines, but it isn't zero-based, so we add 1 to the line number to make sure it isn't the last element to avoid the trailing comma
  
if (count ($lines) != ($index 1))
  {
    
$sql .= ", ";
  }
}

// And query it
mysql_query ($sql);

?>

That works for the most part, It works with a small amount of words but I'm trying to insert 1000's-millions and this script dosnt submit them when I try.
Reply


Messages In This Thread
RE: Submit Textfile Contents To Database - by xbiohazardx - 10-28-2010, 03:19 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP echo contents of folder help DAMINK™ 3 1,249 06-01-2012, 04:07 PM
Last Post: DAMINK™
  PHP error on page submit kaosjon 7 2,722 09-18-2011, 03:31 AM
Last Post: AceInfinity
  PHP automatically create new database after x tables Dutchcoffee 5 1,299 02-25-2010, 05:35 PM
Last Post: Dutchcoffee
  Time added to database Dutchcoffee 1 772 12-30-2009, 05:00 PM
Last Post: Gaijin
  Directory Contents zone 0 569 11-07-2009, 03:38 AM
Last Post: zone

Forum Jump:


Users browsing this thread: 2 Guest(s)