crmepham
crmepham

Reputation: 4740

Verification for delete post function?

I have a simple function to delete a row from a mysql database.

function delete_post($post_id){
        global $dbh;

        $stmt = $dbh->prepare("DELETE FROM mjbox_posts WHERE post_id = ?");
        $stmt->bindValue(1, $post_id, PDO::PARAM_INT);
        $stmt->execute();

    }

I was wondering if there was a simple way within the existing function after the deletion attempt to verify if it was successful? (By successful I mean if the row was deleted)

Upvotes: 0

Views: 57

Answers (2)

Your Common Sense
Your Common Sense

Reputation: 157860

function delete_post($post_id){
    global $dbh;

    $stmt = $dbh->prepare("DELETE FROM mjbox_posts WHERE post_id = ?");
    $stmt->bindValue(1, $post_id, PDO::PARAM_INT);
    $stmt->execute();
    return $stmt->rowCount();
}

However, I wouldn't call unsuccessful a query that didn't deleted anything just because there is no such record.

Upvotes: 1

Xandervr
Xandervr

Reputation: 74

You can select the id of the row before deleting and check if that id exists after deletion.

Upvotes: 0

Related Questions