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。谢谢。