我希望使用PHP在SQL Express服务器中插入批量数据。
我有一个包含数据的csv文件。
我使用下面的代码插入数据,但它只添加了50行。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Upload page</title>
<style type="text/css">
body {
background: #E3F4FC;
font: normal 14px/30px Helvetica, Arial, sans-serif;
color: #2b2b2b;
}
a {
color:#898989;
font-size:14px;
font-weight:bold;
text-decoration:none;
}
a:hover {
color:#CC0033;
}
h1 {
font: bold 14px Helvetica, Arial, sans-serif;
color: #CC0033;
}
h2 {
font: bold 14px Helvetica, Arial, sans-serif;
color: #898989;
}
#container {
background: #CCC;
margin: 100px auto;
width: 945px;
}
#form {padding: 20px 150px;}
#form input {margin-bottom: 20px;}
</style>
</head>
<body>
<div id="container">
<div id="form">
<?php
ini_set('memory_limit', '512M');
ini_set('max_execution_time', '180');
//Upload File
if (isset($_POST['submit'])) {
if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
echo "<h2>Displaying contents:</h2>";
//readfile($_FILES['filename']['tmp_name']);
}
//Import uploaded file to Database
$row = 1;
if (($handle = fopen($_FILES['filename']['tmp_name'], "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
$server = "10.10.10.10\SQLEXPRESS,1433";
$options = array( "UID" => "user", "PWD" => "1234", "Database" => "Analytics");
$conn = sqlsrv_connect($server, $options);
if ($conn == false) die("<pre>".print_r(sqlsrv_errors(), true));echo "Successfully connected!";
$import="INSERT into [Table](column1,column2,column3) values('$data[0]','$data[1]','$data[2]')";
echo $import;
sqlsrv_query($conn,$import) or die(sqlsrv_error());
unset($data);
}
fclose($handle);
}
echo "<br /></p>\n";
echo $row;
print "Import done";
//view upload form
}else {
print "Upload new csv by browsing to file and clicking on Upload<br />\n";
print "<form enctype='multipart/form-data' action='Main.php' method='post'>";
print "File name to import:<br />\n";
print "<input size='50' type='file' name='filename'><br />\n";
print "<input type='submit' name='submit' value='Upload'></form>";
}
?>
</div>
</div>
</body>
</html>
&#13;
如果我通过注释插入命令在php中打印结果,它将显示所有字段。