2012-12-02 51 views
0

我有一個數據庫查詢,它使用ADODB與未命名的佔位符插入數據到數據庫,我試圖將其轉換爲使用PDO,但我得到一個錯誤,這可能是由於語法我在用着。從ADODB遷移到PDO

我試圖:

包含文件中,我有以下共同的功能:

function insCOA($data) { 

    global $dbh; 

    try { 
     $sth=$dbh->prepare(" 
     INSERT INTO 
      coa 
      (
       nom_code, 
       acc_title, 
       acc_type_id, 
       acc_desc 
      ) VALUES (
       ?, 
       ?, 
       ?, 
       ? 
      ) 
     "); 

     for ($i = 0; $i < count($data); $i++) { 
      $sth->execute($data[$i]); 
      $last_insert_id = $dbo->lastInsertId(); 
     } 
    } 

    catch(PDOException $e) { 
     echo "Something went wrong. Please report this error."; 
     file_put_contents('/PDOErrors.txt', $e->getMessage(), FILE_APPEND); 
    } 
    return $last_insert_id; 
} 

在我的PHP頁面我有以下幾點:

// Add to coa table 
$data = array(
    array(
     $nom_code,    /* nom_code   */ 
     $acc_title,    /* acc_title  */ 
     $acc_type_id,   /* acc_type_id  */ 
     $acc_desc    /* acc_desc   */ 
    ) 
); 
$coa_id = insCOA($data); 

連接在其他地方處理並且連接正常。它以$ dbh的全局導出。

我得到的錯誤是

致命錯誤:調用一個成員函數lastInsertId()中的線574 /common.funcs.php非對象上(這是在參考是lastInsertId ()以上

最初,使用ADODB時,共享的功能如下:。

共享功能:

function insCOA($data) { 

    global $conn; 

    $sql = " 
     INSERT INTO 
      coa 
      (
       nom_code, 
       acc_title, 
       acc_type_id, 
       acc_desc 
      ) VALUES (
       ?, 
       ?, 
       ?, 
       ? 
      ) 
    "; 

    for ($i = 0; $i < count($data); $i++) { 
     if ($conn->Execute($sql,$data[$i]) === false) { 
      print 'error' . $conn->ErrorMsg() . '<br />Query: ' . $sql; 
     } else { 
      $last_insert_id = $conn->Insert_ID(); 
     } 
    } 
    return $last_insert_id; 
} 

在PHP頁面沒有陳GED。

一旦我有這個工作,我將能夠轉換一堆其他查詢,所以解決這個問題將非常有用。這是我第一次嘗試使用PDO。謝謝。

回答

0

發現這是錯誤自己:

$dbo->lastInsertId(); 

應該

$dbh->lastInsertId(); 

它現在可以正常使用。