Tuesday, 20 August 2013

mysqli/PDO information: from mysqli to pdo-> how to handle error

mysqli/PDO information: from mysqli to pdo-> how to handle error

I have finished the main functions of my code, but I have got tons of
"useless" code that I'm trying to remove.
This is an example:
else if(isset($_POST['act']) && $_SESSION['status']<3 &&
$_POST['act']=='delete_ticket'){
$encid=preg_replace('/\s+/','',$_POST['enc']);
$encid=($encid!='' && strlen($encid)==87) ? $encid:exit();
$mysqli = new mysqli($Hostname, $Username, $Password, $DatabaseName);
$stmt = $mysqli->stmt_init();
if($stmt){
$query = "UPDATE ".$SupportTicketsTable." a
INNER JOIN ".$SupportUserTable." b
ON b.id=a.operator_id
SET b.assigned_tickets= CASE WHEN
b.assigned_tickets!='0' THEN (b.assigned_tickets-1)
ELSE b.assigned_tickets END
WHERE a.enc_id=?";
if($prepared = $stmt->prepare($query)){
if($stmt->bind_param('s', $encid)){
if($stmt->execute()){
$query = "DELETE FROM ".$SupportMessagesTable." WHERE
`ticket_id`=(SELECT `id` FROM ".$SupportTicketsTable."
WHERE `enc_id`=?) ";
if($prepared = $stmt->prepare($query)){
if($stmt->bind_param('s', $encid)){
if($stmt->execute()){
$query = "SELECT enc FROM
".$SupportUploadTable." WHERE
`ticket_id`=?";
if($prepared = $stmt->prepare($query)){
if($stmt->bind_param('s', $encid)){
if($stmt->execute()){
$stmt->store_result();
$result =
$stmt->bind_result($mustang);
if($stmt->num_rows>0){
$path='../upload/';
while
(mysqli_stmt_fetch($stmt))
{
if(file_exists($path.$mustang)){
file_put_contents($path.$mustang,'');
unlink($path.$mustang);
}
}
}
$query = "DELETE FROM
".$SupportUploadTable." WHERE
`ticket_id`=?";
if($prepared =
$stmt->prepare($query)){
if($stmt->bind_param('s',
$encid)){
if($stmt->execute()){
$query = "DELETE
FROM
".$SupportFlagTable."
WHERE `enc_id`=?";
if($prepared =
$stmt->prepare($query)){
if($stmt->bind_param('s',
$encid)){
if($stmt->execute()){
$query
=
"DELETE
FROM
".$SupportTicketsTable."
WHERE
`enc_id`=?";
if($prepared
=
$stmt->prepare($query)){
if($stmt->bind_param('s',
$encid)){
if($stmt->execute()){
echo
json_encode(array(0=>'Deleted'));
}
else
echo
json_encode(array(0=>mysqli_stmt_error($stmt)));
}
else
echo
json_encode(array(0=>mysqli_stmt_error($stmt)));
}
else
echo
json_encode(array(0=>mysqli_stmt_error($stmt)));
}
else
echo
json_encode(array(0=>mysqli_stmt_error($stmt)));
}
else
echo
json_encode(array(0=>mysqli_stmt_error($stmt)));
}
else
echo
json_encode(array(0=>mysqli_stmt_error($stmt)));
}
else
echo
json_encode(array(0=>mysqli_stmt_error($stmt)));
}
else
echo
json_encode(array(0=>mysqli_stmt_error($stmt)));
}
else
echo
json_encode(array(0=>mysqli_stmt_error($stmt)));
}
else
echo
json_encode(array(0=>mysqli_stmt_error($stmt)));
}
else
echo
json_encode(array(0=>mysqli_stmt_error($stmt)));
}
else
echo
json_encode(array(0=>mysqli_stmt_error($stmt)));
}
else
echo
json_encode(array(0=>mysqli_stmt_error($stmt)));
}
else
echo
json_encode(array(0=>mysqli_stmt_error($stmt)));
}
else
echo json_encode(array(0=>mysqli_stmt_error($stmt)));
}
else
echo json_encode(array(0=>mysqli_stmt_error($stmt)));
}
else
echo json_encode(array(0=>mysqli_stmt_error($stmt)));
}
else
echo json_encode(array(0=>mysqli_stmt_error($stmt)));
}
else
echo json_encode(array(0=>mysqli_stmt_error($stmt)));
exit();
}
As you can see there are too many if statement for the mysqli connection:
this is the part I want to remove, is there a way remove them? I have
tried to wrap it between try and catch but the code returns anyway the
success echo (it's an AJAX call).
I have read that mysqli have got some problems with try and catch function
so I have took a look at PDO, but I'm not an expert and I don't understan
if I can get the same result of the code that I have posted with it, I
have also read that I have to change the error mode to
PDO::ERRMODE_EXCEPTION, but I haven't understood very well the point.
Basically: is there someone who can explain me how PDO handle the errors?

No comments:

Post a Comment