Php批量插入

时间:2012-12-05 07:41:01

标签: php bulkinsert output

我有使用批量插入的PHP代码。我运行代码并且没有错误。,问题没有OUTPUT这个代码和空白页/屏幕出现..我想做的就是拥有使用此代码输出页面和数据库..

 <?php

     $dbh = odbc_connect(
           "DRIVER={SQL Server Native Client 10.0};Server=.;Database=ECPNWEB", 
           "sa", "ECPAY");




 if (($handle = fopen("c:\\tblmcwd.txt", "r")) !== FALSE) {
     while (($data = fgetcsv($handle, 4096, "|")) !== FALSE) {
         if (count($data) == 10) {
             $sql = "INSERT INTO [dbo].[tblMCWD] (
                         [ID], 
                         [ConsumerCode], 
                         [ConsumerName], 
                         [AccountStatus], 
                         [AccountNumber], 
                         [DueDate], 
                         [CurrentBill], 
                         [PreviousBill], 
                         [TotalDiscount], 
                         [TotalGrossAmountDue]
                     ) VALUES (
                         ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
                     )";

             $stmt = $dbh->prepare($sql);
             $stmt->bindValue(1, $data[0]);
             $stmt->bindValue(2, $data[1]);
             $stmt->bindValue(3, $data[2]);
             $stmt->bindValue(4, $data[3]);
             $stmt->bindValue(5, $data[4]);
             $stmt->bindValue(6, $data[5]);
             $stmt->bindValue(7, $data[6]);
             $stmt->bindValue(8, $data[7]);
             $stmt->bindValue(9, $data[8]);
             $stmt->bindValue(10, $data[9]);
             $stmt->execute();
         }
     }
     fclose($handle);
 }
 ?>

1 个答案:

答案 0 :(得分:1)

试试这个:

....

$sql = "INSERT INTO [dbo].[tblMCWD] (
                     [ID], 
                     [ConsumerCode], 
                     [ConsumerName], 
                     [AccountStatus], 
                     [AccountNumber], 
                     [DueDate], 
                     [CurrentBill], 
                     [PreviousBill], 
                     [TotalDiscount], 
                     [TotalGrossAmountDue]
                 ) VALUES (
                     :ID, :ConsumerCode, :ConsumerName, :AccountStatus, :AccountNumber, :DueDate, :CurrentBill, :PreviousBill, TotalDiscount, TotalGrossAmountDue
                 )";
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':ID', $data[0]);
$stmt->bindParam(':ConsumerCode', $data[1],PDO::PARAM_STR);
$stmt->bindParam(':ConsumerName', $data[2],PDO::PARAM_STR);
$stmt->bindParam(':AccountStatus', $data[3],PDO::PARAM_STR);
$stmt->bindParam(':AccountNumber', $data[4],PDO::PARAM_STR);
$stmt->bindParam(':DuaDate' , $data[5],PDO::PARAM_STR);
$stmt->bindParam(':CurrentBill', $data[6],PDO::PARAM_STR);
$stmt->bindParam(':PreviousBill', $data[7],PDO::PARAM_STR);
$stmt->bindParam(':TotalDiscount', $data[8],PDO::PARAM_STR);
$stmt->bindParam(':TotalGrossAmountDue', $data[9],PDO::PARAM_STR);
$stmt->execute();

if(!$stmt){    // Check if the query executed succesfull, if not, print the data...
    $printedString = "ID: %1$s ConsumerCode: %2$s ConsumerName: %3$s"; // and so on....
    $printedString = sprintf($printedString , $data[0],$data[1],$data[2]); // and so on...
} else{
    echo "Everything executed succesfully!<br />";
}

您可以在此处找到更多信息:Link 在这里:Link 2 在这里:Link 3

对字符串使用PDO::PARAM_STR,对整数使用PDO::PARAM_INT,并尝试一下