从访问数据库返回id

时间:2016-10-18 13:07:08

标签: php mysql ms-access phpmyadmin

这是我使用php将数据插入访问数据库的代码。

$conn = new COM ("ADODB.Connection") or die("Cannot start ADO");

$connStr = "PROVIDER=Microsoft.Ace.OLEDB.12.0;Data Source=" . realpath(‘my access path’) . ";";
// Open the connection to the database
$conn->open($connStr);


$query = “my insert query here which inserts into theaccess database fine”

$query2 = "select @@IDENTITY"

try{
$rs = $conn->execute($query);

$idReturned = $conn->lastInsertId();

echo json_encode($idReturned); 

} catch(com_exception $e){
        echo($e);
    }

我正在尝试获取返回的ID,但我得到的只是以下错误:

  

异常'com_exception',消息'Source:ADODB.Connection   描述:参数类型错误,不可接受   范围,或彼此冲突。在   C:\ inetpub \ wwwroot \ agency \ createnewvaluation.php:132堆栈跟踪:#0   C:\的Inetpub \ wwwroot的\代理商\ createnewvaluation.php(132):   com-> lastInsertId()#1 {main}

1 个答案:

答案 0 :(得分:0)

我手动完成了结果并自己获得了代码

if($dbh->getAttribute(PDO::ATTR_DRIVER_NAME) == 'pgsql') {

} elseif($dbh->getAttribute(PDO::ATTR_DRIVER_NAME) == 'odbc') {
  $sb = $dbh->prepare('SELECT @@IDENTITY AS lastID');
  $sb->execute();
  $row = $sb->fetch(PDO::FETCH_ASSOC);
  $arr = array("ref" => $row["lastID"]);
                echo json_encode($arr);
} else {
                $arr = array("ref" => "error");
                echo json_encode($arr);
}