Creating INSERT INTO statement from dbf file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Ave,

I¹ve written a code that is able to extract the Column names and Records
from a simple dbf (foxpro) file and create an INSERT INTO sql statement
which can be used to insert all those records with their corresponding field
names in an existing mySQL table. (A CREATE TABLE code I wrote is able to
create the table from the dbf file information).

Following is the code I wrote for creating the INSERT INTO sql:

<?php
            $db_path = "$DATABASEFILE";
            $dbh = dbase_open($db_path, 0) or die("Error! Could not open
dbase database file '$db_path'.");
            if ($dbh) {
            
            #Get the Information
            $column_info = dbase_get_header_info($dbh);
            $record_numbers = dbase_numrecords($dbh);
            
            #Run the loop for all the records in the Table
            for ($i = 1; $i <= $record_numbers; $i++) {
            $row = dbase_get_record_with_names($dbh, $i);
            
            echo "INSERT INTO ".substr($DATABASEFILE_name,0,-4)." (";
            
            #Run the loop for all the fields in the Table
            foreach ($column_info as $v1) {    echo "$v1[name],";    }
            
            echo ") VALUES (";

            #Run the loop for all the values corresponding to fields in the
Table
            foreach ($column_info as $v1) {    echo
"'".trim($row[$v1[name]])."',";    }
                   
            echo "'); <br>";
                   
            }
            }
            dbase_close($dbh);
?> 

It works fine, except for one problem. It¹s able to create the INSERT INTO
sql statement, with all the fields and corresponding values, but as I¹m
running a loop for both the fields names, and the values corresponding to
fields names, it leaves a comma after the records are over.

So instead of having this : INSERT INTO tblname (c1,c2,c3) VALUES
(?v1¹,¹v2¹,¹v3¹);
I achieve this : INSERT INTO tblname (c1,c2,c3,) VALUES (?v1¹,¹v2¹,¹v3¹,¹);

Notice an additional Comma after column names, and an additional ,¹ after
the values. I¹m not quite sure what to do to get rid of those. I¹ve tried
some different combinations using different kind of logic with the echo
statements, but it¹s not working out. Would love some help.

Thanks,

Rahul S. Johari
Coordinator, Internet & Administration
Informed Marketing Services Inc.
251 River Street
Troy, NY 12180

Tel: (518) 266-0909 x154
Fax: (518) 266-0909
Email: rahul@xxxxxxxxxxxxxxxxxxxx
http://www.informed-sources.com


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux