根据post的id从oracle查询相关姓名,年龄然后再批量insert到mysql,如何返回插入成功的数据量?
如下插入不成功,有更优方案吗?
<?php
require_once dirname(__FILE__).'/../common.php';
session_start();
$id = $_POST['id'];
$id_clause = "'" . str_replace("\n", "','", $id) . "'"; //前端换行传来的id分隔处理
require '../connect_oracle.php'; //链接oracle数据库
//通过id从oracle查询数据
$sql1 ="select a.id,a.name,a.age,a.mark from (
select t.id,t.name,t.age,t.mark,to_char(input_time, 'yyyy-mm-dd hh24:mi:ss')as
new_time,row_number() OVER(PARTITION BY id ORDER BY t.input_time desc) as row_flg from table t where t.id in ($id_clause) and t.mark='0') a where a.row_flg = '1'";
$success = array();
$failed = array();
foreach ($conn->query($sql1,PDO::FETCH_ASSOC) as $res) {
$id = $res['ID'];
$name= $res['NAME'];
$age= $res['AGE'];
$mark = $res['MARK'];
require '../pdo_connect_db.php';//链接mysql
//插入mysql
$result = $pdo->exec("INSERT INTO mytable (id,name,age,mark)
values(
'$id',
'$name',
'$age',
'$mark')");
}
if ($result > 0)
{
$success[] = array('code' => 0, 'msg' => '发送成功');
}else{
$failed[] = array('code' => 1, 'msg' => '发送失败');
}
echo json_encode(array_merge($success, $failed));
?>