Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    PHP




Importing A Flat Pipe Delimited File Into A MySQL Data Table


I have done this in perl but eeew I don't want to use perl anymore. I
want to be able to take a flat file that looks like this:

mbriones@...|Marian|Briones
bobsmith@...|Bob|Smith
janedoe@...|Jane|Doe
(etc)

The table has more than 3 fields, so I'd want to process the flat file
and then do an INSERT into mailinglist SET field=$value for each line.

Does someone have a nice way for me to do this? The flat file will be
uploaded to the server via a form (copy) and then be processed.




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Importing To DB From Pipe Delimited Text File
I am trying to make a PHP script that will import data from a pipe delimited text file in to a database. The problem I am having is that the fields have long descriptions in them and there are multiple line breaks for paragraphs. They contain HTML codes as well (<B>).

The multiple line breaks are causing it to import the data strange, putting data in the wrong fields and totally missing some data as well. Here is what the text file Code:

Tab Delimited Txt File To Php Html Table
Wouldn't you know that when I finally find a tutorial using Google that does exactly what I want, the code doesn't run when I use it ...

However, when it gets to this line, for ($i=0; $i<td>$line[0]</td>, it chokes on one of the '>'.

Since my php skills are very basic, and in no way include arrays, can someone assist me with this?

If it's easier to do it differently, what I'd like to do is create a table in Excel/Access, export it to the server (to a tab-delimted text file, since I'll be using commas in my table cells), and have it display in an HTML table.

If I can't figure this out, I'm tempted to just export to HTML from one or the other program (not sure quite yet which I would use), so ...

Multiple Table Insert From Tab Delimited File
I have a page I'm coding for batch updates from a tab delimited file. Each row has data that has to be checked against and inserted/updated on different tables in my mySQL database.

How would I go about parsing through the tab delimited file and then storing data between the tabs in order to insert individually into separate tables??

Here's what I have so far...

$contents = file('/myfile.txt');

for ($i=0; $i<sizeof($contents); $i++) {

$line = trim($contents[$i]);
$arr = explode(" ", $line);

$line[$i] = array('ID' => $arr[0], 'Record_Type' => $arr[1], 'First_Name' => $arr[2], 'MI' => $arr[3], 'Last_Name' =>
$arr[4], 'Firm' => $arr[5], 'Address' => $arr[6], 'type' => rtrim($arr[13]));

I need to take the different columns.. ID, Record_Type, etc. and update different tables with each value where the IDs match.

Importing Data Into Mysql
I'm starting working on a new project where I'll have to allow users to upload and import tab-formatted text files into the db. Should I use the LOAD DATA INFILE from PHP or write a custom procedure?

Importing Data To MySql From Web Page
I'm using Drupal(a CMS), along with MySQL and PHP to import data into a database.  I'm trying to import a bunch of sentences on one page into the database, but each in their own row.  For example:

News
-----
Brand new site
New employee hired
New work acquired

Basically, I need to know how to pull those into the database as one row each, instead of pulling all three elements into the database in the same row.  They're all stored in a $content tag, b/c I wrote the page in PHP.  So, any ideas on how to make it, so I can insert the $content into the database, but at a line break, I need to insert a new row?

Importing To Mysql Without LOAD DATA LOCAL
I've just gone through the learning curve of realizing most virtual hosts have "LOAD DATA LOCAL" disabled :(

I have a two meg delimited ascii file I need to import on a regular basis (26 fields)

Anyone have a nice import routine using fopen they are willing to share before I attempt to hack one out myself? I can't seem to find such an animal out there (though some are close).

Such a shame, from what I have read, load data is much faster...

Importing Large Amounts Of Data Via File Upload
I was wondering how anyone here deals with move large amounts of
mysql data to be uploaded to the server? Large as in 75MB CSV file. I
have a client who wants to admin there database with 15,000 users.
They manage all data offline and want to upload to the site for
display purposes only. My server has 5MB php file upload limit. I
really do not want to up this limit.

What I am wondering is how does cPanel or PHP MyAdmin get around this
upload limit using php scripting?

Displaying Data From A Flat File?
ive got a flatfile database sorted out, all i need now is how to store individual flat files per user and how to paginate that data. Just to make sure i'm doing everything right, i'll post the code below Code:

Flat File And Passing Data
I am currently working on a project whereby users login to my website (which is working) and then using a form add new links/tabs to their navigation bar (which i need help developing). So for instance if the user had 'home/search engines/favourites' as links on their navigation bar and they wanted to add auction sites as a group title they fill out the form which wants the name of the link and the URL it goes to.

So far i can make the form send the link name and URL to my flat file database (usertabs.txt) but i also need the form to once submitted automatically insert the users username before the link and URL to make their new link unique to them. So in the flat file the structure will be:

username|Link name|URL

The usernames etc are stored in a separate .txt file.
I have this code so far which is the basis for uploading new links to a users navigation bar. $userid is the variable i want to be associated with the user who is logged on. Code:

Deleting Strings Of Data In Flat File Db
I have a flat file database with some data stored in it. I have a script which uploads new data to it and want to have a script where by i can delete specific rows of data.

I have a 'delete' link which when clicked is meant to delete that specific row of data which works when each columb of data is a single word with no spaces but when i have strings of data it won't delete.

What Functions Should I Use To Fetch Data From A Flat File ?
What functions should i use to fetch data from a flat file ?

Importing Csv Text File Into MySQL
I have a CSV file delimited by ~. Here is an example of 3 records: Code:

Importing Text File Into MySQL DB
I'm writing a script to import a text file with fields delimited by ~ into a MySQL DB. I'm trying to import everything into an array first, so that one array row = one record row. Here is some dummy data, with the &#390170;' record having a couple of new lines where a new paragraph is. Note the other records have NULL in the same field: PHP Code:

Pulling Date-specific Data From Flat File
I'm almost a complete newbie to using PHP and I was hoping one of you
guys can point me in the right direction. I'm working on a site for a
brand of handmade fruit juice and it only needs one bit of dynamic
content: the info on what fruit is in the juice and where it is coming
from (vendor, country etc.) today and tomorrow (the content of the
juice is different everyday). So I thought it would be easiest to put
that information in a flat file (an Excel file would be ideal but a
plain text would do too) on a weekly basis and try to pull out the
needed info with PHP. Except I don't know how yet. I don't expect any
of you to do the work for me but if someone could point me to which
functions of PHP I should do some research on, I'd be most grateful.

Importing Excel File Into Mysql Database
I need help regarding importing excel file into mysql database.I am using PHP 5.0.2, mysql 4.2.13. I need code for importing excel file into mysql database and what will be the steps for it.

Site Stats, Php/mysql OR Flat Text File?
I'm have a cms  and I want site stats built in, thus apache's stats or cpanel etc don't cut it for multiple users.

I have a working stat query below, the problem is, in a short time 12 hours, it's grown to 109k with 500 records.
now I'm not adding a row for every person, I create a session, and if they already have a row, I update clicks (page clicks) as you can see.

still, it's big, the bots are eating it, so I figure, why not just create a separate table for bots, with 1 row per day and a click count. there, smaller. but with multiple users, and lots of normal hits, this will still grow quite large very fast!

so I'm wondering if flat text files would be a better solution? but perhaps opening and writing to a text file constantly gets very slow? I'm confused. Code:

Problems While Exporting Data Using Pipe Symbols
I am facing problems while trying to export data into
pipe format. In fact I am fetching records from mysql database and then
trying to create a text file ( using pipe as delimeter) , intending to
open in Excel.

The data is been inserted from textarea fields, where the
user can write text and may press enter key to format his text
accordingly. Now when the data entered into the database it keeps some
unidentified value so as to keep the formatting. Hence while displaying
the text file I find that lines are seperated according to the format
user gave. Thus creating the problem while opening up in the excel
file.

example:

Using PHP To Sort Data In MySQL Table 1 By Values In Table 2
I have a MySQL database with 2 tables in it. products and prices.. Products has a field in it called 'prodno'. Prices has a field in it called 'prodid'.

There is one of each item in products and each product has it's own unique 'prodno'

There are multiple instances of each product's pricing in prices.. One for each price.. So if a product had multiple prices depending on quantity it would have an entry for each price.. Example:

id = 1
prodid = 7001
qty = 300
price = 12.5

id = 2
prodid = 7001
qty = 400
price = 15.5

I was wondering if I could query the database getting info from the products table but sort it according to the prices in the prices table.

Kinda like doing a "SELECT * FROM products" ordering it by the lowest price value from each item..

This is all very confusing to me, and I'm the one writing it. Let's try one last time..

Query the database selecting * from products (I plan on using all the info in products) and ordering them by the lowest price for each item in the prices table. I've tried sorting the results of just a basic SELECT * FROM products using Javascript and PHP but with pagination in the results it makes it a bit hard.

Importing Data
I have this file cvs that's roughly 2.5 meg. Right now, it uses a function like this to import the data into the table. PHP Code:

Importing From CSV Using LOAD DATA
what attributes should i use for a text coloum should it be binary?

Importing Data From Excel Into PHP
Is it possible to import data from excel file into php and than put it in
mysql database?

Importing Form Data To Ms Access
Does anyone know if one can import a web form data into ms access db? Does the ms access db have to be on the same server as the web form?

Q. Data Not Posting Mysql Table
I am trying to post from a form to table using the code below from file2.php
and having two problems:

1. The data is not being posted to the table
2. After submit is pressed the following message comes up: "The page cannot
be displayed" - The browser can't find the file, because it is looking for
http://file2.php instead of http://teachercards.org//file2.php

Adding Data To Mysql Table
I have the following script which is supposed to process a complete order for a serial number from a POST. It has so far been working okay. It processes the order successfully, then sends an email to me and to the user, but it is failing at the point where it has to do the insert of the data into the CustomersTable. PHP Code:

Comma Delimited File
I am trying to read comma delimited rows of text. The problem is that
some fields may be encapsulated in "" - particularly the text fields but
not numeric fields.

Is there a simple efficient way to parse the fields with comma but also
strip off the "" encapsulating some of the fields with php?

The problem is the "" encapsulation is optional. Some fields will have
it, some won't.

Delimiting A Non-delimited File
I have a text file, with addresses of business' that I would like to convert to a more useable format ie CSV. Currently there is no delimiting of the various address components.

"A 1 AUTO SALES 1224 S BROADWAY ST WICHITA, KS 67211-3124 316-263-8748"
"ABERLE FORD INC 1025 MAIN ST SABETHA, KS 66534-1893 913-284-3122"
"ACTION AUTO RENTAL & SALES 1101 W 4TH AVE HUTCHINSON, KS 67501 316-662-3699"

As you can see from the examples above the only consistent items are the state and the phone number, and the city is always followed by a comma. I am new to regex and the whole range of string handling functions and I am not sure where to start.

Checking For Existing Data In Mysql Table..
I'm having a bit of a problem on a project I'm working on.. Here's the deal: I want to log search terms. Easy enough, right? Well, to make things interesting, I want to *NOT* log terms less than 3 chars long, *AND* if the term's already in the database, I don't want to add the term again, I just want to update the 'count' field to reflect that there was a 'hit' to the word.

Here's how I coded it note: it doesn't work because I suck and I have no real idea how to do what I'm trying to do): This is the actual "recording" code I came up with:

if(strlen($term)>3){
$record_term = sql_query("insert into $terms(ID, Term, count) values ('', '$term','')");

Now, this is the code I've come up for for the adding to the count part.. (and I know it's wrong, if someone can fix that too, I'd appreciate it):

$query = mysql_query("SELECT term, count FROM Term WHERE term like $term;");
$entry = @mysql_fetch_array($query);

$new_count = $entry["count"]+1;
$query = mysql_query("UPDATE links SET count = $new_count, WHERE Term like $term;");
mysql_close($db);

-----------------------------------------
So how do I combine these two actions to do what I want it to do? and what's wrong with the second query?

Replace A Cell In A Table With New Data In MySQL?
how to replace a cell in a table with new data in MySQL? -OR- A good site that will explain it step by step and not just list functions? For example, I have a change username page, Need it to change the username field in the users function as a result.

PHP Form Overwrites MySQL Table Data
I have a PHP form sending data to a MySQL table, and I am wondering if I can add some sort of code to the PHP page that will make the form data overwrite any potentially old data already in the table.

Transferring Excel Data To Mysql Table
Is it possible to transfer the whole data from excel sheet to the mysql database?

Reading Tab Delimited File Into Database
I have a .tab file that I need to write to the database. I've read it in, but for some reason it's not reading correctly. I'm truncating the complete database and then reloading it with whatever is in the tab file. I keep getting an error when trying to run my current code which is: Code:

Read Data From A Table In Pdf File And Store It To Database
I have to populate MySQL database where data will come from a PDF file. This PDF file has a well formatted table and I have to make a similar replica of the table data into MYSQL Database. Is that possible at all?

Newbie Question: How Do I Fill A Table With Data From Mysql
I have a database that i hook into using php and I want to create a loop
that will create a table an populate it with data from the table but I
really am at a loss on how to achieve this.

How To Update Mysql Table Without Changing Existing Data
how to update mysql table without changing existing data.

Retrieve Data From Mysql Table And Put A Hyperlink For Download
I have created a database and a table and have stored the information in mysql table.
Now my question is how do I retrieve that data from the my sql table and have a php page that when someone goes to say:download.php, he/she will be able to download the data from my mysql table? Also what if I have multiple tables in mysql that I want to be able to download from the php file?

Match Input Value With Values In A Delimited File
I'm very very new to php. I need your help. I have a text file which is ~ delimited. Each line has a name and link. I want to check if the link is similar to my input and if it is then return the name.

Stripping Quotes Out Of A Comma Delimited File?
I have a flat file that I'm trying to stick into a MySQL database. One
record per line, multiple fields per record, and many of them are null
fields which are just double quotes without a space between. It's probably
nothing really major for people who have done this before, but I'm a bit
stumped. The file is comma delimited. Every field is surrounded by double
quotes. I've done quite a bit of searching, on the php site and elsewhere,
but I can't seem to get it to strip the quotes out so I can explode the file
line by line to grab the fields.

Reading Excel To MySql Or Comma Delimited ...
right direction on how to deal with an xls
flie.

but the only other file I have to work
with is pdf.

I'm not sure, is xls the better of two evils?


CSV Importing To MySQL
Well I had the last script working perfectly. Except sometimes we rearrange the CSV "headers" so a field may be in one place on one file and another place on another file, yet they are the same thing. I've tired about 30 different scripts and none of them work at all. For example:

Csv file looks like:
work order,client name,start date,end date
7283728,"name,client",02/07/07,04/02/07

that is just the test file I've made up. I found out, the hard way, you cannot have spaces in your database fields. So what I am trying to do is take that first line, replace all spaces with an _. That away database fields can be like work_order and in the CSV file also. I tried using Code:

Importing Xml To Mysql
i am using this piece of code to import an xml file to mysql.

$feed[$key] = $info;

is causing the script to only return the last element of the xml file.

I would like to return all elements but cant find a way to do so,

<?php
    include 'library/config.php'
    include 'library/opendb.php'
$file = "my-xml.xml";

$feed = array();
$key = "";
$info = "";


function startElement($xml_parser,  $attrs ) {
  global $feed;  
   }

function endElement($xml_parser, $name) {
  global $feed,  $info;
   $key = $name;
  $feed[$key] = $info;
  $info = ""; }

function charData($xml_parser, $data ) {
  global $info;
  
  
  $info .= $data; }
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "charData" );
$fp = fopen($file, "r");
while ($data = fread($fp, 8192))
!xml_parse($xml_parser, $data, feof($fp));
xml_parser_free($xml_parser);

$sql= "INSERT INTO section ( ";
$j=0;
$i=count($feed);

foreach( $feed as $assoc_index => $value )
  {
  $j++;
  $sql.= strtolower($assoc_index);
  if($i>$j) $sql.= " , ";
  if($i<=$j) {$sql.= " ) VALUES ('";}
  }
$h=0;
foreach( $feed as $assoc_index => $value )
  {
  $h++;
  $sql.= utf8_decode(trim(addslashes($value)));
  if($i-1>$h) $sql.= "', '";
  if($i<=$h) $sql.= "','');";
  }
  $query=trim($sql);
  echo $value;
if (mysql_query($sql)){
echo "success in table creation.";
} else {
echo "no table created.";
}
echo mysql_error();
  echo $sql;
?>

Duplicate Records In DB When Importing From CSV File
The code below is used to grab each row from a CSV file (except the
first row) and insert it into a database, as long as it's not already
in the DB.

If I echo each row's data, it appears only once on the page. But, for
some reason it inserts each row into the database 3 times. The code
that echos the data, and the DB insertion code all appear within the
same for loop.

The database insertion code (below) works perfectly in another script.
It's just copied/pasted into the for loop.

$row = 0;
$handle = fopen ("some-file.csv","r");
while ($data = fgetcsv ($handle, 1000, ",")) {
if($row == 0){
// set the varible array
$num = count ($data);
$csvNames = array();
for ($c=0; $c < $num; $c++) {
$csvNames[$c] = $data[$c];
eval("$" . $data[$c] . " = array();");
}
$row = 1;
}else{
$num = count ($data);
$row++;
for ($c=0; $c < $num; $c++) {
$buildEval = "$" . $csvNames[$c] . " = "" . $data[$c] .
"";";
eval($buildEval);
echo $buildEval . "<br>"; // this properly displays only one
row's data from the CSV file, then displays the next row in the CSV
file when it loops again.

// BUT FOR SOME REASON, THE DB CODE BELOW INSERTS EACH ROW FROM THE
CSV FILE 3 TIMES.

// Set variables with current date and time to be inserted into DB
later
$appDate = date('Y-m-d');
$appTime = date('H:i:s');

// Prepare the phone numbers for easy input into the database further
down this page.
$fHomePhoneCombined = $fHomePhone1 . "-" . $fHomePhone2 . "-" .
$fHomePhone3;
$fBusinessPhoneCombined = $fBusinessPhone1 . "-" . $fBusinessPhone2 .
"-" . $fBusinessPhone3;

// Connect to the database
$db = mysql_connect ("localhost", "username", "password");
mysql_select_db ("mydatabase");

// Check to see if the record is new,
$newRecord = 1;
$queryNewRecord = mysql_query("SELECT * FROM cust_test WHERE
first_name = '$fFirstName' AND last_name = '$fLastName' AND email =
'$fEmail'");
if (mysql_num_rows($queryNewRecord) > 0){
$newRecord = 0;
}

// If it's NOT a new record, insert the contents into cust_test_bad DB
// I'll insert this code later. It's not important for this test.

// Write the information to the customers table, if it's a new record.

if ($newRecord == 1){
$query = "INSERT INTO cust_test SET
application_date = '$appDate',
application_time = '$appTime',
first_name = '$fFirstName',
last_name = '$fLastName',
email = '$fEmail',
address = '$fAddress',
state = '$fState',
zip = '$fZip',
home_phone = '$fHomePhoneCombined',
best_time_to_call = '$fBestTime',
lead_buyer = '$lbuy'";


$result = mysql_query($query) or die('Failed because:
'.mysql_error());
}

} // end of for ($c=0; $c < $num; $c++)
}
}
fclose ($handle);

Importing Excel Into A MySQL Database
i have an excel file I want to read into a database.  I found a tutorial which says to save the file into a xml file, then it basically gets all the data out, and puts it into an array Code:

Importing A Field From MySQL Into A Variable In Flash?
I have a query that is giving me the field that I require from my database using PHP.
What is the best way to import this variable into flash? Can I echo the number out on the page and have Flash import it that way? Could I use the Load Variable command? code:

Mysql Table Vs Log File
I have been doing some searching for  a tutorial on creating a simple tracking script to attach to each of my pages.  The tutorials that I found are using a log.txt file instead of a mysql database.  can anyone explain why not use a mysql database

Exporting DB Results (or Flat File) To An Excel File
I have two scripts, neither of which have been written, that will need to collect user information. They will either write to a flat file or to a MySQL DB, and I need a way for an administrator to grab an Excel file based on this data.

First, no more than 100 people are expected to sign up, so flat file is certainly viable. Do you recommend I use DB or flat file?

Second, based on your recommendation, how should I go about exporting the user data to an Excel file? There will be about ten form fields per registration to give you an idea.

Create A Table In MySQL From A .php File
Is it possible to create a mySQL table from a .php file? If so how?

Importing .sql File Replaces Special Characters (á, ç Etc.) With Questions Marks
I am trying to import an .sql file into a new instalation, but the resultant table replaces special characters (á, ç etc.) with questions marks.

Editing entries manually via PhpMyAdmin doesn't work either.

Destination versions:
phpMyAdmin 2.6.1
MySQL 4.1.9-max

Downloading File Stored In Mysql Table Through Ssl
I have a table where I store some binary data. To retrieve this
information as a downloadable file, I use the following code:

header('Content-Type: $mimeType');
header("Content-length: $size");

// for filename, i store the filename in the table, so this does not
actually
// point to a physical file name on the file system
header("Content-Disposition: attachment; filename=$fileName");
header("Content-Description: PHP Generated Data");
echo $data;
return true;

If I go through a normal port (http), the file downloads no problem.
When I try this through ssl (https), I get the following error:

'Internet Explorer cannot download / from 192.168.0.9.'

Through port 80, the filename is set correctly. Through ssl, the
filename doesn't seem to be getting set. Is there a problem with
using 'Content-Disposition: attachment' through ssl?

Flat File
I run a website for a small (very small) AM radio station. I need to add a page where listener's (all seven of them) can list items for sale. (This will be the online version of a daily call-in "For Sale" show that the station runs.) The station owner wants the listings to disappear automatically after 3 days.

My current plan is to let the visitor enter info on a form: telephone no. and item description. The form will tack on a timestamp and then append the data to the target file.

When displaying the data, I need to:

1. Only display data less than 3 days old and...

2. Delete from the file data more than 3 days old.

I'm thinking of having the display page read the file into an array and compare timestamps to a standard. Those that pass are copied into a temporary file. When the entire array has been checked, the temp file is written over the original target file. The temp file is discarded and the new, most current version of the target file is displayed on screen.

Does this seem like a reasonably logical way to do this? I looked at the PHP manual and didn't see a command that removes single entries from a flat file, so this is the approach I came up with. But perhaps I missed something?

I'm using a flat file because I don't anticipate ever having more than just a handful of entries at a time. (I also just want the practice with flat files; I haven't used them as much as I have relational databases.)

A couple of other notes:

Users will not have to have a password or anything like that. They'll just post and the item will stay on the board for three days, as per the station owner's wishes.

The system is being kept simple in part because the owner believes that most of the listeners are elderly and (in his estimation) not super computer types.


Copyright © 2005-08 www.BigResource.com, All rights reserved