根据选择更新表,可能使用锁定?

I'm using php and am trying select the following with mysql:

$state_id = $sql->query('select state_id from table1 where id='.$sql->escape_string($_REQUEST['id']));

if ($state_id == $_REQUEST['state_id']) {
    $res = $sql->query('update table1 set state_id=state_id+1, state="'.$sql->escape_string($_REQUEST['state']).'" where id='.$sql->escape_string($_REQUEST['id']));
    echo 1; // Passes
} else {
    echo 0; // Fails
}

I want the update to only happen if the state_id is equals to $_REQUEST['state_id'] but I also want to return the result of this condition to the client, all of this being done in one atomic operation. My thoughts were maybe I could lock the table1 table and unlock it before echoing 0 or 1.

What could be a better way of doing this?

I think you might want a "SELECT .. FOR UPDATE":

If you are using InnoDB tables:

http://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.html