I have a query which contains 'INSERT','DELETE' and 'UPDATE' statement. Now my question is "Is it possible no statement run if any other of the statement is failed"
$sql=" START Transaction INSERT statement; DELETE statement; UPDATE statement; COMMIT";
You must use a MySQL transactions. It will let you to roll back all changes (which was made on transaction) if something goes wrong (e.g. you got an error).
Simple example:
<?php
$all_query_ok=true; // our control variable
$mysqli = new mysqli("localhost", "user", "pass", "dbname");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s
", mysqli_connect_error());
exit();
}
/* disable autocommit */
$mysqli->autocommit(FALSE);
//we make 4 inserts, the last one generates an error
//if at least one query returns an error we change our control variable
$mysqli->query("INSERT INTO myCity (id) VALUES (100)") ? null : $all_query_ok=false;
$mysqli->query("INSERT INTO myCity (id) VALUES (200)") ? null : $all_query_ok=false;
$mysqli->query("INSERT INTO myCity (id) VALUES (300)") ? null : $all_query_ok=false;
$mysqli->query("INSERT INTO myCity (id) VALUES (100)") ? null : $all_query_ok=false; //duplicated PRIMARY KEY VALUE
//now let's test our control variable
$all_query_ok ? $mysqli->commit() : $mysqli->rollback();
$mysqli->close();
?>