简体   繁体   中英

Using PHP variable variables in building a MySql INSERT

I am working on replacing a short script where I was using LOAD DATA INFILE to rapidly insert a CSV into a MySQL database using PHP.

I am completely stumped and would appreciate some assistance - I have been looking at this for hours and have simply confused myself!

The issue is that I do not know the number of columns in the CSV nor do I want to define them as they are liable to change.

Get the headers from CSV:

// Get the first column of the file to create the column headers
$fp = fopen($upload_folder . $tempName . '.csv', 'r');
$frow = fgetcsv($fp, 100000, ',');

// Set import strings
$columns = '';
$data_string = '';

$col_count = 0;

foreach($frow as $column) {
  // Build import strings
  $columns .= "`$column` varchar(50)"; 
  $data_string .= "'$" . "data[" . $col_count . "]', ";

  // Now increment the column count
  $col_count++;
}

Import: (I have echo'd this as I do not want it to run!)

    while (($data = fgetcsv($fp, 100000, ",")) !== FALSE) {
       echo "INSERT INTO nav_data_" . $tempName . " (" . $columns . ") VALUES (" . $data_string . ") ";
    }

Problem: Whilst the column names are properly constructed the second $data[0], $data[1] that is called in the WHILE statement are not parsed so I get:

VALUES ('$data[0]', '$data[1]', '$data[2]', '$data[3]', '$data[4]', '$data[5]', '$data[6]' ... '$data[25]')

Where as I want the output from $data = fgetcsv($fp, 100000, ",") ...

VALUES ('1', 'Farming', 'Tree', 'Tractor', '345', 'etc.')

I know this is to do with PHP Variable variables but for the life of me I cannot work this out.

Example CSV data import: (As requested)

    Document Type   No_ Order Date  Shipment Date   Sell-to Customer No_    Sell-to Customer Name   Bill-to Customer No_    Bill-to Name    Ship-to Code    Order Status    Customer Disc_ Group    Salesperson Code    Entered by  Taken at    External Document No_   Taken By    Sample  Payment Method Code Currency Code   Payment Terms Code  Amount  O/S Amount Ex VAT (LCY) No_ Order Lines No_ Complete Order Lines    Delivery Date


    Order   SO006906    02/12/14    15/04/15    C001293 Amazon  C001293 Amazon  DEL Insufficient Credit     HS  SARAH   EMAIL           0   ACCOUNT     30D 2313    2313    15  11  


    Order   SO007198    13/01/15    01/04/15    C000871 Amazon  C000871 Amazon  DEL Awaiting Order Ack.     SUSP    SARAH   FF15    RESERVE SUSP    0   ACCOUNT     30DS2.5/30  688 688 4   0   


    Order   SO008292    01/05/15    19/10/15    C003075 Amazon  C003075 Amazon  DEL Proforma Ready To Ship      ZP  SARAH   REP         0   PROFORMA        PROFORMA    484.8   484.8   21  0   

I think that your problem is around $data_string .= "'$" . "data[" . $col_count . "]', "; $data_string .= "'$" . "data[" . $col_count . "]', "; . Can I suggest creating a $column array and imploding it after your loop to prevent the comma on the end.

$column = array();
foreach($frow as $column) {

  // Build import strings
  $column[] = "`$column` varchar(50)"; 

  // Now increment the column count
  $col_count++;
}
$columns = implode(',',$column);

Then, generate your insert value string in your while loop using $data and $col_count in the same way as above.

while (($data = fgetcsv($fp, 100000, ",")) !== FALSE) {

   //$insertColumn = array();
   //for($i=0;$i<=$col_count;$i++){
   //     $insertColumn[] = $data[$i];
   //}
   //$insertColumnString = implode("','",$insertColumn);
   $insertColumnString = implode("','",$data);

   echo "INSERT INTO nav_data_{$tempName} ($columns) VALUES ('$insertColumnString')";

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM