选择interbase并仅插入mysql show 1记录

时间:2014-04-30 02:50:19

标签: php mysql pdo insert interbase

我尝试从另一台使用interbase的计算机上获取数据。我使用PDO将interbase值插入Mysql。但只有一条记录插入到mysql中。为什么呢?

$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);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

尝试以下

// 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;
        }
    }
}

我不完全确定1062是要检查的错误代码。过去,我使用1586来检测唯一约束违规。试错将发现正确的检查价值。

您还使用$stmt->errorInfo作为属性而不是方法$stmt->errorInfo()