0

I'd like to use transaction in my code. But it doesn't work.

In my simple base i have table called "persons" with 2 columns (person_id, name) and it has no data yet.

mysql_query("SET AUTOCOMMIT=0");
mysql_query("START TRANSACTION");

$a1 = mysql_query("DELETE FROM persons WHERE person_id='5'");
$a2 = mysql_query("DELETE FROM persons WHERE person_id='1'");

if ($a1 and $a2) {
  mysql_query("COMMIT");
  echo "this is commit";
} else {        
mysql_query("ROLLBACK");
echo "this is rollback";
} 
}

When i'm deleting person which have 5th and 1st person_id, it should goto the ROLLBACK because i have no data in base(base is totally empty), but browser is displaying "this is commit".

How can i fix this transaction.

Qaisar Satti
  • 2,742
  • 2
  • 18
  • 36
  • 1
    please stop using `mysql_*` functions. Use `mysqli_` or PDO – Alex May 14 '15 at 18:26
  • in mysqli_ this problem is not exist ? – Andrzej Opiela May 14 '15 at 18:28
  • there is no problem with php core functions. the problem is developpers who are using those functions in wrong way. – Alex May 14 '15 at 18:31
  • when you do `mysql_query("DELETE` that will return true even if you not actually deleted any rows. – Alex May 14 '15 at 18:32
  • 1
    possible duplicate of [What does a successful MySQL DELETE return? How to check if DELETE was successful?](http://stackoverflow.com/questions/922398/what-does-a-successful-mysql-delete-return-how-to-check-if-delete-was-successfu) – Alex May 14 '15 at 18:33

1 Answers1

1

By using mysql_affected_rows we can check if rows are affected or not.

Mnks
  • 64
  • 5