[英]Insert Multi Row Oracle result set into MYSQL using PHP
A little background. 一点背景。 I have an Oracle database that I am trying to query and then insert into a local MYSQL database so that I can generate canned reports. 我有一个要查询的Oracle数据库,然后将其插入本地的MySQL数据库中,以便生成罐头报告。 I have been trying to figure out this insert into Mysql for a while now. 我已经试图找出这个插入Mysql了一段时间了。 I have the Oracle portion running correctly but when I try to insert I have been getting a syntax error in mysql. 我的Oracle部分运行正常,但是当我尝试插入时,我在mysql中遇到语法错误。
The result set comes back with 8 rows the first of which is the Key in MYSQL. 结果集返回8行,其中第一行是MYSQL中的Key。 I would really like to convert this insert query I built into a insert on duplicate key update statement but am lost on how I would do this properly. 我真的很想将我构建的这个插入查询转换为重复键更新语句中的插入内容,但是对于如何正确执行此操作却迷失了。 Any help you guys can provide would be appreciated. 大家可以提供的任何帮助将不胜感激。
$db1 = '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=HOST)(PORT = 1521))(CONNECT_DATA=(SERVICE_NAME=Service)))';
$c1 = oci_connect("Userid", "Pass", $db1);
$sql = oci_parse($c1, "select statement") ;
oci_execute($sql);
$i = 0;
while ($row = oci_fetch_array($sql)){
$i++;
$k = $row[0];
$dte = $row[1];
$cus = $row[2];
$odr = $row[3];
$lin = $row[4];
$cas = $row[5];
$lpo = $row[6];
$cpl = $row[7];
$cpo = $row[8];
};
$db_user = "userid";
$db_pass = "Pass";
$db = new PDO('mysql:host=host; dbname=databasename', $db_user, $db_pass);
$stmt = $db->prepare("INSERT INTO `cuspi` (k, dte, cus, odr, lin, casa, lpo, cpl, cpo) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
$recordcount = count($k);
for ($i = 0; $i < $recordcount; $i++) {
$records[] = [
$k[$i],
$dte[$i],
$cus[$i],
$odr[$i],
$lin[$i],
$casa[$i],
$lpo[$i],
$cpl[$i],
$cpo[$i],
];
}
foreach ($records as $record) {
$stmt->execute($record);
}
?>
I was able to figure out the Answer. 我能够弄清楚答案。 I was missing the grave accent around the column references for the insert. 我缺少有关插入的列引用周围的重音符号。
Original 原版的
$stmt = $db->prepare("INSERT INTO `cuspi` (k, dte, cus, odr, lin, casa, lpo, cpl, cpo) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
Fixed 固定
$stmt = $db->prepare("INSERT INTO `cuspi` (`k`, `dte`, `cus`, `odr`, `lin`, `casa`, `lpo`, `cpl`, `cpo`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.