Return IDs of inserted values PDO to MSSQL using O

2020-05-09 11:59发布

问题:

I need to get an array of id's of the subscribers added to the database within this function:

function insert_test($pdo, $fullname, $email) {
if ($SQL = $pdo->prepare("INSERT INTO subscribers ([dateAdded],[dateUpdated],[fullname],[email],[isActive]) VALUES (GETDATE(), GETDATE(), :fullname, :email, 1)")) {
    $SQL->bindValue(':fullname', $fullname, PDO::PARAM_STR);
    $SQL->bindValue(':email', $email, PDO::PARAM_STR);
    $SQL->execute();

    return array('status'=> true);
} else {
    return array('status'=> false);
}
}

However I am stuggling with figuring out how to get the values and return them within the array like so:

return array('status'=> true,'ids'=> $ids);

I have read up and found that SCOPE_IDENTITY() is probably the most reliable way of getting these values but I don't know where to put it in this function to return the values that I need. The Primary key in the database is the id column.

Would it be something like this:

if ($SQL = $pdo->prepare("INSERT INTO subscribers ([dateAdded],[dateUpdated],[fullname],[email],[isActive]) VALUES (GETDATE(), GETDATE(), :fullname, :email, 1) SELECT SCOPE_IDENTITY()"))

And then bind a parameter after that called $ids or am I over thinking this completely?

Any help would be greatly appreciated!

EDIT: I have tried using a similar function to the one in this question and was receiving an invalid cursor error so adapted it to look like this (notice the closeCursor was how to fix the invalid cursor error):

function insert_test($pdo, $fullname, $email) {
    if ($SQL = $pdo->prepare("INSERT INTO subscribers ([dateAdded],[dateUpdated],[fullname],[email],[isActive]) OUTPUT INSERTED.id VALUES (GETDATE(), GETDATE(), :fullname, :email, 1)")) {
        $SQL->bindValue(':fullname', $fullname, PDO::PARAM_STR);
        $SQL->bindValue(':email', $email, PDO::PARAM_STR);
        $SQL->execute();
        $SQL->closeCursor();
        $ids = $SQL->fetchAll(PDO::FETCH_ASSOC);

        return array('status'=> true, 'id' => $ids);
    } else {
        $pdo = null;
        return array('status'=> false);
    }
}

The return from this function is now this:

Array
(
    [status] => 1
    [id] => Array
        (
        ) 
)

So it seems the output is not working as it should? This is getting stranger and stranger...

回答1:

Figured it out guys!

There was a problem with the position of the closeCursor(); method the function now looks like this:

function insert_test($pdo, $fullname, $email) {
    if ($SQL = $pdo->prepare("INSERT INTO subscribers ([dateAdded],[dateUpdated],[fullname],[email],[isActive]) OUTPUT INSERTED.id VALUES (GETDATE(), GETDATE(), :fullname, :email, 1)")) {
        $SQL->bindValue(':fullname', $fullname, PDO::PARAM_STR);
        $SQL->bindValue(':email', $email, PDO::PARAM_STR);
        $SQL->execute();    
        $ids = $SQL->fetchAll(PDO::FETCH_ASSOC);
        $SQL->closeCursor();

        foreach ($ids as $id) {
            return $id['id'];
        }

    } else {
        $pdo = null;
        return false;
    }
}

So when insert_test($pdo, $fullname, $email); is within a for each loop containing the data being inserted the function returns each id as desired.

If anyone can see any inherent problems with this please let me know!