Skip to content

Instantly share code, notes, and snippets.

@itswadesh
Created March 27, 2017 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itswadesh/05fc73e037042f5d5e21eb1c0186f0c9 to your computer and use it in GitHub Desktop.
Save itswadesh/05fc73e037042f5d5e21eb1c0186f0c9 to your computer and use it in GitHub Desktop.
function delete($table, $where){
if(count($where)<=0){
$response["status"] = "warning";
$response["message"] = "Delete Failed: At least one condition is required";
}else{
try{
$a = array();
$w = "";
foreach ($where as $key => $value) {
$w .= " and " .$key. " = :".$key;
$a[":".$key] = $value;
}
$stmt = $this->db->prepare("DELETE FROM $table WHERE 1=1 ".$w);
$stmt->execute($a);
$affected_rows = $stmt->rowCount();
if($affected_rows<=0){
$response["status"] = "warning";
$response["message"] = "No row deleted";
}else{
$response["status"] = "success";
$response["message"] = $affected_rows." row(s) deleted from database";
}
}catch(PDOException $e){
$response["status"] = "error";
$response["message"] = 'Delete Failed: ' .$e->getMessage();
}
}
return $response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment