Reply to Re: [PHP] Creating INSERT INTO statement from dbf file

Your name:

Reply:


Posted by Satyam on 04/05/05 11:45

You can either put a flag indicating your first time around the field loop
and add the comma before each field, when it is not the first time around,
or build the fieldname and fieldvalue list on a string and then trim out the
last character once the loop is finished.


"Rahul S. Johari" <rjohari@nycap.rr.com> wrote in message
news:BE771CDB.A0FA%rjohari@nycap.rr.com...

Ave,

Ive 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. Its able to create the INSERT INTO
sql statement, with all the fields and corresponding values, but as Im
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
(Ov1,v2,v3);
I achieve this : INSERT INTO tblname (c1,c2,c3,) VALUES (Ov1,v2,v3,);

Notice an additional Comma after column names, and an additional , after
the values. Im not quite sure what to do to get rid of those. Ive tried
some different combinations using different kind of logic with the echo
statements, but its 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@informed-sources.com
http://www.informed-sources.com

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация