从ADODB迁移到PDO

从ADODB迁移到PDO,pdo,prepared-statement,adodb,Pdo,Prepared Statement,Adodb,我有一个数据库查询,它使用带有未命名占位符的ADODB将数据插入数据库,我试图将其转换为使用PDO,但我得到一个错误,可能是由于我使用的语法 我正在尝试的是: 在包含的文件中,我具有以下共享功能: function insCOA($data) { global $dbh; try { $sth=$dbh->prepare(" INSERT INTO coa (

我有一个数据库查询,它使用带有未命名占位符的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;
}
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;
}
连接在别处处理,并且连接正常。它以全局格式导出为$dbh

我得到的错误是

致命错误:在第574行/common.funcs.php中的非对象上调用成员函数lastInsertId()(此处引用的是上面的lastInsertId()

最初,使用ADODB时,共享函数如下所示:

共享功能:

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;
}
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;
}
函数insCOA($data){
全球$conn;
$sql=”
插入
辅酶A
(
名称代码,
行政协调会名称,
acc_类型_id,
附件说明
)价值观(
?,
?,
?,
?
)
";
对于($i=0;$iExecute($sql,$data[$i])==false){
打印“错误”。$conn->ErrorMsg()。
查询:。$sql; }否则{ $last_insert_id=$conn->insert_id(); } } 返回$last\u insert\u id; }
在PHP页面中,没有任何更改


一旦这项工作正常,我将能够转换一系列其他查询,因此解决这一问题将非常有用。这是我第一次尝试使用PDO。谢谢。

我自己发现了错误,错误是:

$dbo->lastInsertId();
应该是

$dbh->lastInsertId();
它现在工作得很好