Current location: Hot Scripts Forums » Programming Languages » PHP » daily import text file to mysql database


daily import text file to mysql database

Reply
  #1 (permalink)  
Old 01-05-10, 12:23 AM
syaifuri syaifuri is offline
New Member
 
Join Date: Jan 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
daily import text file to mysql database

Please advice,

I need a php script that can generate import text.txt to mysql database daily; the purpose is updating database refer to the logfile of tester machine at my production lines.

text.txt is the logfile tester machine, and line.txt is the temporary text file that used for update the latest line of text.txt that already upload to mysql.
The question is,
How to make a procedure at the next execution of the script will begin from the last line of line.txt. so it will not duplicate with the previous data imported.

and below is the script usage;
=============
PHP Code:

<?
include("config.php");
//class untuk export file
class exportFile
{
    var 
$Query_ID=0;
    var 
$connection=0;

    function 
connect()
    {
        if(
$this->connection==0) {
            
$this->connection=mysql_connect(HOST,USERNAME,PASSWORD) or die("<b>Database Error</b><br>".mysql_error());
            
$SelectResult mysql_select_db(DB$this->connection) or die("Couldnot Select Database".mysql_error());
        } else {
            echo 
"Connection Couldnot be Established";
            die();
        }
    }
    
    function 
query($sql) {
        
$this->Query_ID=mysql_query($sql,$this->connection);
        if(!
$this->Query_ID) {
            echo 
"Query Failed".mysql_error();
        } else {
            return 
$this->Query_ID;
        }
    }

    function 
exportFileToDatbase($filename,$de,$mode,$tablename,$fieldno,$numLines)
    {
        
$fd=fopen($filename,"$mode");
        while(!
feof($fd)) {
            
$line=fgets($fd,5000);
            
$f=explode($de,$line);
            for(
$i=0;$i<$fieldno;$i++) {
                    
$a[]=trim("'$f[$i]'");
                }    
            
$value=implode(",",$a);
            unset(
$a);
            
$sql="insert into $tablename values($value)";
            
//echo $sql;
            
$this->query($sql);
            
        }
        
    }
    
}

$objFile=new exportFile;
$objFile->connect();

//baca temporary line.txt
$line_array file("line.txt");
$bytes=$line_array[0];

// read the file into an array with each line being a seperate element
// count the number of elements (lines)
$fileName "text.txt";
$fileArray file$fileName );
if( 
$fileArray ) {
$numLines count$fileArray );
}
// statement to compare line usage "text.txt" with temporary "line.txt"
if ($numLines != $bytes)
{
$objFile->exportFileToDatbase("text.txt",",","r","sample",4,$numLines); 
// File name,Seprator,mode,tablename,field 
//save the latest line to "line.txt"
$UO_Log 'line.txt';
$UO_NewRecord .= "$numLines";
$UO_Handle fopen($UO_Log'w');
fwrite($UO_Handle$UO_NewRecord);
fclose($UO_Handle);
}
else { 
    echo 
"jumlah line =";
        print 
$numLines ;
        echo 
" data is similar with the previous hours";
      } 

?>

Last edited by Nico; 01-05-10 at 05:40 AM.
Reply With Quote
  #2 (permalink)  
Old 01-11-10, 05:35 AM
dgreenhouse's Avatar
dgreenhouse dgreenhouse is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: San Francisco
Posts: 457
Thanks: 0
Thanked 3 Times in 3 Posts
It's impossible to tell from what you've provided, but the quick answer is...

You'll need to know where one ends and the other begins...
Reply With Quote
Reply

Bookmarks

Tags
text to mysql


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using data stored in a text file Philmo PHP 3 04-08-08 01:20 PM
Generate HTML output with PHP and mySQL zoliky PHP 3 10-03-06 05:26 AM
Search script improvement 9999 PHP 14 08-28-06 11:46 PM
Changing where Flash pulls text file - how? jdsmith8 Flash & ActionScript 4 08-02-06 10:12 AM
Need to submit form to database and text file - Any ideas how? dpreiss ASP 1 08-21-03 06:02 PM


All times are GMT -5. The time now is 08:06 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.