-1

I want to know if there is a better way to find if a row was deleted using PDO. What am I doing now is to read first from database and then delete. If read operation returns content I display success message and if not I display data not fount message.

This is my code:

$find_it = 0;
    $content = $cancel_db->select_all_general("SELECT * FROM dm_bookings WHERE id = '$c_reference' AND email = '$c_email'");
    foreach ($content as $row){
        $find_it = 1;
    }
    $cancel_db->insert_update_delete_general_no_m("DELETE FROM dm_bookings WHERE id = '$c_reference' AND email = '$c_email'");
    if($find_it == 1) {
        $div_class = "alert-success";
        $mesaj = "<br />Booking deleted!<br /><br />";
    } else {
        $div_class = "alert-danger";
        $mesaj = "<br /Booking not finded!<br /><br />";
    }

Is there a way to improve this? Thanks.

Your Common Sense
  • 154,967
  • 38
  • 205
  • 325

2 Answers2

0

Using PDO in PHP gives you the ability to use rowCount(); which returns a count of all rows affects by the last query.
Something like the code below would be able to check to see if it was deleted.

<?php

/* This will only check if a total of 1 was deletd, however, to change this replace == to > */
if($pdo->rowCount() == 1){
   /* Row Deleted */
} else {
   /* Row was not deleted */
}

?>
FluxCoder
  • 1,236
  • 10
  • 22
-2

Have a look at PDO::exec, first example is what you are looking for :

/* Delete all rows from the FRUIT table */
$count = $dbh->exec("DELETE FROM fruit");

/* Return number of rows that were deleted */
print("Deleted $count rows.\n");
Will
  • 879
  • 6
  • 15