Created
March 27, 2017 16:21
-
-
Save itswadesh/05fc73e037042f5d5e21eb1c0186f0c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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