这是我的mysql查询代码,我想更改为sqlsrv查询以导入到sql server数据库
$message = null;
$allowed_extensions = array('csv');
$upload_path = '../csv';
if (!empty($_FILES['file'])) {
if ($_FILES['file']['error'] == 0) {
// check extension
$file = explode(".", $_FILES['file']['name']);
$extension = array_pop($file);
if (in_array($extension, $allowed_extensions)) {
if (move_uploaded_file($_FILES['file']['tmp_name'], $upload_path.'/'.$_FILES['file']['name'])) {
if (($handle = fopen($upload_path.'/'.$_FILES['file']['name'], "r")) !== false) {
$keys = array();
$out = array();
$insert = array();
$line = 1;
while (($row = fgetcsv($handle, 0, ',', '"')) !== FALSE) {
foreach($row as $key => $value) {
if ($line === 1) {
$keys[$key] = $value;
} else {
$out[$line][$key] = $value;
}
}
$line++;
}
fclose($handle);
if (!empty($keys) && !empty($out)) {
$db = new PDO('mysql:host=localhost;dbname=project', 'root', '');
$db->exec("SET CHARACTER SET utf8");
foreach($out as $key => $value) {
$sql = "INSERT INTO `department` (`";
$sql .= implode("`, `", $keys);
$sql .= "`) VALUES (";
$sql .= implode(", ", array_fill(0, count($keys), "?"));
$sql .= ")";
$statement = $db->prepare($sql);
$statement->execute($value);
}
$message = '<span style="color:green">File has been uploaded successfully</span>';
}
}
}
} else {
$message = '<span style="color:red">Only .csv file format is allowed</span>';
}
} else {
$message = '<span style="color:red">There was a problem with your file</span>';
}
答案 0 :(得分:0)
希望它能帮助您手动完成。
有时MS SQL表或列名称用方括号括在查询中(例如,如果包含空格或出于某些其他原因)。 MySQL不允许在列名表周围使用方括号,它们都必须由symbol or cut off: [object] ->
object`替换。
所以你需要删除所有&#34;&#39;&#34;在上面的例子中,也来自表名部门及其列。 更多。
http://www.convert-in.com/mssql-to-mysql-queries.htm
添加了:
您的SQL查询如下所示。请删除 ?标记也是。
$sql = INSERT INTO department (;
$sql .= implode(",", $keys);
$sql .= ) VALUES (";
$sql .= implode(", ", array_fill(0, count($keys), "?"));
$sql .= ")";
$statement = $db->prepare($sql);
$statement->execute($value);
我对PHP不太了解。我只能帮助构造SQL查询。它应该是
"INSERT INTO department (COLUMN1,COLUMN2,..) values(VALUE1,VALUE1,..)".
添加了:
正如我已经告诉过你的那样,我不是一个PHP家伙。但是为你而战。请尝试您的PHP代码:
$sql = "INSERT INTO department (";
$sql .= "implode(',', $keys)";
$sql .= ") VALUES (";
$sql .= "implode(',', array_fill(0, count($keys), ''))";
$sql .= ")";
$statement = $db->prepare($sql); $statement->execute($value);
但 array_fill(0,count($ keys),&#39;&#39;)是空数据吗?