Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.