2

this is my code that is inserting data into an access database using 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);
    }

I’m trying to get the returned id but all I am getting is the below error :

exception 'com_exception' with message 'Source: ADODB.Connection Description: Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.' in C:\inetpub\wwwroot\agency\createnewvaluation.php:132 Stack trace: #0 C:\inetpub\wwwroot\agency\createnewvaluation.php(132): com->lastInsertId() #1 {main}

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
Alex Banerjee
  • 474
  • 4
  • 15

1 Answers1

0

I went though the results manually and got the code myself

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);
}
Alex Banerjee
  • 474
  • 4
  • 15