
January 16th, 2013, 08:43 AM
|
|
Contributing User
|
|
Join Date: Feb 2008
Posts: 76
Time spent in forums: 1 Day 1 h 42 m 42 sec
Reputation Power: 6
|
|
|
PHP5 - Import a csv file in to an oracle database
I have a PHP page which browses for a csv file. I want to import that file into an oracle table.
PHP Code:
$Import = (isset($_POST['import']) ? $_POST['import'] : null );
if ($Import){
echo "file is ";
$source=$_POST['file1'];
echo $source. "<br>";
$row=0;
if (($handle = fopen($source, "r")) !== FALSE) {
$size=filesize($source)+1;
while (($data = fgetcsv($handle, $size, ",")) !== FALSE) {
if ($row==0) {
$col=count($data);
echo "Header is ";
for ($h=0; $h<$col; $h++){
echo $data[$h]. ",";
}
}
if ($row <> 0) {
$col=count($data);
echo $col."fields in row".$row;
for ($c=0; $c<$col; $c++){
echo $data[$c]. ",";
$sql= "INSERT INTO TEMP_AFF VALUES ('$data[0]', '$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]')";
$stid = oci_parse($conn,$sql);
$exec =oci_execute($stid,OCI_DEFAULT);
}
}
echo "<br />\n";
$row++;
}
}
if (!$exec) {
$e = oci_error($stid);
trigger_error(htmlentities($e['message']), E_USER_ERROR);
}
$commit=oci_commit($conn);
fclose($handle);
oci_free_statement($stid);
}
In my test file I have the column header and 2 more rows of data.
It is importing it but instead of 2 rows its bringing in 16 rows. Its my for loop which is causing the problem. As I loop through each col, I seem to be adding a row to the database. Its something stupid but I cant fix it.
|