我正在尝试以下方法:
<?php
$sql = "SELECT * FROM (SELECT ID, `LAST NAME` AS lm, `FIRST NAME` AS fm FROM `OLD`";
$sql .= " UNION ALL SELECT ID, `Last Name` AS lm, `First Name` AS fm, FROM `NEW`) ";
$sth = $dbh->prepare($sql);
$params = array();
$sth->execute($params);
$ids = $lm = $fm = $dp = $ex = $bp = $em = array();
$counter = 0;
while ($row = $sth->fetch()) {
$ids[] = $row['ID'];
$lm[] = $row['lm'];
$fm[] = $row['fm'];
$counter++;
}
for($j=0;$j < $counter;$j++) {
$new = "INSERT INTO MYTABLE (ID, lastname, firstname)";
$new.= " VALUES( '".$ids[$j]."', '".$lm[$j]."', '".$fm[$j]."')";
$stmt = sqlsrv_query($conn, $new);
if( !$stmt ) {
die( print_r( sqlsrv_errors(), true));
}
}
?>
这显示以下错误:
数组([0] =&gt;数组([0] =&gt; 42000 [SQLSTATE] =&gt; 42000 [1] =&gt; 102 [代码] =&gt; 102 [2] =&gt; [微软] [SQL Server的ODBC驱动程序11] [SQL 服务器]&#39;附近的语法不正确&#39;。 [message] =&gt; [Microsoft] [ODBC驱动程序 11 for SQL Server] [SQL Server]&#39;附近的语法不正确&#39;。 ))
在我的for循环中,如果我尝试回显$ lm [$ j]和$ fm [$ j]变量,我会正常得到我的结果,但由于某种原因查询没有执行。
有什么想法吗?
提前谢谢