65220

Php Bulk insert

Question:

I have the code with php using bulk insert.,I run the code and there is no error., The Problem there is no OUTPUT with this code and blank page/screen appear .. All I want to do is to have the Output with the page and with the database using this code ..

<?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); } ?>

Answer1:

Try this:

.... $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 />"; }

More info can you find here: <a href="http://php.net/manual/en/function.sprintf.php" rel="nofollow">Link</a> And here: <a href="http://php.net/manual/en/pdostatement.execute.php" rel="nofollow">Link 2</a> And here: <a href="http://php.net/manual/en/pdostatement.bindparam.php" rel="nofollow">Link 3</a>

Use PDO::PARAM_STR for strings and PDO::PARAM_INT for an integer, and give it an try

Recommend

  • AWS CloudFormation: Passing Values between Nested Stacks
  • How to implement backward compatible soap webservice (java based)?
  • npm start - npm ERR! missing script: start
  • WordPress - I can't login - Something wrong with the wp_users table
  • Rails4: To check for no image uploaded with carrierwave is not working when delete the image
  • IndexAxisValueFormatter not working as expected
  • CMake cl.exe is not able to compile a simple test program
  • Can't receive notification from GCM
  • Jittering when moving mouse
  • How to have an EditText stuck to the soft Keyboard on Android
  • handling sigterm in OSx
  • Cross Domain Authentication using DirectoryServices
  • In Mapbox.js, how to smooth a polyline?
  • Internet Explorer 10 back button caching
  • Grails error: table or view does not exist
  • Check overlap begin and end time by group in R
  • Get-AzureRmAppServicePlan and Get-AzureRmWebApp return exceptions in Runbook
  • dbus Variant: How to preserve boolean datatype in Python?
  • double precision error when converting to scientific notation
  • Pass pointer array to function
  • python & Mysql: unsupported operand type(s) for -: 'int' and 'tuple'
  • get src attribute of an image based on class or id
  • Serve file to user over http via php
  • Unix Network Programming Clarification
  • Embedded Glassfish JPA Datasource connection fail
  • Private IP address in reserved subnet range
  • Specify the x-axis values in ggplot in R
  • Security issues with PHP's Readfile method
  • Transactional Create with Validation in ServiceStack Redis Client
  • Custom Tabgroup Appcelerator
  • Django rest serializer Breaks when data exists
  • why do I get the error when installing the gem 'pg'? [duplicate]
  • Handling un-mapped Rest path
  • How to rebase a series of branches?
  • PHP - How to update data to MySQL when click a radio button
  • Azure Cloud Service Web Role web pages do not load
  • How to check if every primary key value is being referenced as foreign key in another table
  • Sending data from AppleScript to FileMaker records
  • what is the difference between the asp.net mvc application and asp.net web application
  • How to get NHibernate ISession to cache entity not retrieved by primary key