【问题标题】:Migrating from ADODB to PDO从 ADODB 迁移到 PDO
【发布时间】:2012-12-02 02:43:17
【问题描述】:

我有一个数据库查询,它使用带有未命名占位符的 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。

我得到的错误是

致命错误:在第 574 行的 /common.funcs.php 中的非对象上调用成员函数 lastInsertId()(这是对上面 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 页面中没有任何变化。

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

【问题讨论】:

    标签: pdo prepared-statement adodb


    【解决方案1】:

    自己发现错误是:

    $dbo->lastInsertId();
    

    应该是

    $dbh->lastInsertId();
    

    现在运行良好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-26
      • 2010-10-20
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      • 1970-01-01
      • 2018-06-02
      • 2013-06-17
      相关资源
      最近更新 更多