I try to get data from another machine which use interbase. I'm using PDO to insert the interbase value to Mysql. But just one record which inserted to mysql. Why?
$stmt = "SELECT FIRST 10 TRIM(LINE_NAME) as LINE_NAME, TRIM(MODEL_NAME) as MODEL_NAME, TRIM(PROD_NO) as PROD_NO,
TRIM(LOT_SIZE_IN) as LOT_SIZE_IN,TRIM(START_SERIAL) as START_SERIAL,
TRIM(SERIAL_NO_LOW) as SERIAL_NO_LOW, TRIM(SERIAL_NO_UP) as SERIAL_NO_UP, TRIM(PROD_DATE) as PROD_DATE
FROM DOC_TO";
$sth = ibase_query($dbh, $stmt);
// CREATE AN ARRAY OF OBJECTS
if($sth === FALSE) {
die(ibase_errmsg()); // TODO: better error handling
}
while ($row = ibase_fetch_object($sth)) {
$newvalues[] = $row;
}
$dbh = new PDO('mysql:host=localhost;dbname=qdbase', 'root', 'JeinQA123');
$sql="TRUNCATE TABLE `schedule`";
$stmt = $dbh->query($sql);
if($stmt){echo "connected";}
else{echo "failed";}
// PREPARE A QUERY
$qry2 = "INSERT INTO schedule (line,model,lotno,qty,serial,seriallow,serialup,date )
VALUES (:LINE_NAME, :MODEL_NAME, :PROD_NO,:LOT_SIZE_IN,:START_SERIAL,:SERIAL_NO_LOW,:SERIAL_NO_UP,:PROD_DATE)";
$stmt = $dbh->prepare($qry2);
// USE THE ARRAY OF OBJECTS
foreach ($newvalues as $obj)
{
// BIND THE OBJECT PROPERTIES INTO THE QUERY
$stmt->bindParam('LINE_NAME', $obj->LINE_NAME);
$stmt->bindParam('MODEL_NAME', $obj->MODEL_NAME);
$stmt->bindParam('PROD_NO', $obj->PROD_NO);
$stmt->bindParam('LOT_SIZE_IN', $obj->LOT_SIZE_IN);
$stmt->bindParam('START_SERIAL', $obj->START_SERIAL);
$stmt->bindParam('SERIAL_NO_LOW', $obj->SERIAL_NO_LOW);
$stmt->bindParam('SERIAL_NO_UP', $obj->SERIAL_NO_UP);
$stmt->bindParam('PROD_DATE', $obj->PROD_DATE);
/*I try to add this inside foreach*/
try
{
$stmt->execute();
}
// IF THERE IS AN EXCEPTION THROWN BY PDO
catch (PDOException $exc)
{
// IF THE EXCEPTION NUMBER IS NOT "DUPLICATE UNIQUE"
if ($exc->errorInfo[1] != 1062)
{
$msg = "FAIL: $qry " . PHP_EOL . $exc->getmessage();
trigger_error($msg, E_USER_ERROR);
}
}
}
Try the following
// make sure PDO is set to throw exceptions
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare($qry2);
foreach ($newvalues as $obj) {
try {
$stmt->execute(array(
':LINE_NAME' => $obj->LINE_NAME,
':MODEL_NAME' => $obj->MODEL_NAME,
':PROD_NO' => $obj->PROD_NO,
':LOT_SIZE_IN' => $obj->LOT_SIZE_IN,
':START_SERIAL' => $obj->START_SERIAL,
':SERIAL_NO_LOW' => $obj->SERIAL_NO_LOW,
':SERIAL_NO_UP' => $obj->SERIAL_NO_UP,
':PROD_DATE' => $obj->PROD_DATE
));
} catch (PDOException $e) {
$errorInfo = $stmt->errorInfo();
if ($errorInfo[1] != 1062) {
throw $e;
}
}
}
I'm not entirely sure that 1062
is the error code to be checking for. In the past, I've used 1586
to detect unique constraint violations. Trial and error will discover the right value to check.
You were also using $stmt->errorInfo
as a property instead of the method $stmt->errorInfo()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.