Pretty sure I don't fit into the 'guru' category, but it occurs to me that this doesn't seem like a situation which requires transactions: because you want to execute the
SELECT query all the time, and not have it fail if the
UPDATE fails. Instead, I would simply do this:
PHP Code:
$query = "SELECT * FROM superv_details WHERE (emp_id='$emp_number')";
$res = mysql_query($query);
if($res) { //because mysql_query() will return FALSE if a SELECT query has an empty result set (or on error)
$query = "UPDATE superv_details SET
sperv_name='$fullname', dept_code='$department', emp_status='$emp_status', email='$email_addr'
WHERE emp_id='$emp_number'";
mysql_query($query);
}
However, if you really need to ensure that no one is reading from the table while the script is running, you can obtain a lock which is more portable than a transaction - because it works with all MySQL table types.
PHP Code:
// ... same as above ...
if($res) {
mysql_query('LOCK TABLES superv_details WRITE'); //now no one can read OR write from the table until it is unlocked.
if (!mysql_error()) {
$query = "UPDATE superv_details SET
sperv_name='$fullname', dept_code='$department', emp_status='$emp_status', email='$email_addr'
WHERE emp_id='$emp_number'";
mysql_query($query);
mysql_query('UNLOCK TABLES'); //unlock tables, don't forget this!!
}
else {
print mysql_error();
exit;
}
}