是否可以返回上次删除的记录ID?

时间:2012-08-16 07:59:43

标签: php pdo

我想为我的类创建一个删除一条记录的方法,这里是源代码:

/* Delete One Record
 * in @param (string) $tbl - name of the table
 * in @param (int) $idn - id of record
 * @return (int) - identifier of removed record
 */
public function DelOne($tbl,(int)$idn)
{

    if ($result = $this->pdo->prepare("DELETE FROM `".$tbl."` WHERE `id`=:idn"))
    {

        $result->bindValue(":idn",$idn,PDO::PARAM_INT);

        $result->execute();

    }

}

我希望这个函数返回一个刚刚删除的记录的标识符,而不是标准的TRUE / FALSE组合。

1 个答案:

答案 0 :(得分:3)

在函数中添加return $idn

public function DelOne($tbl,(int)$idn)
{

    if ($result = $this->pdo->prepare("DELETE FROM `".$tbl."` WHERE `id`=:idn"))
    {

        $result->bindValue(":idn",$idn,PDO::PARAM_INT);

        // if all is well, return $idn
        if($result->execute()) return $idn;

    }

    // if we are here, something was wrong
    return false;

}