{"id":2999,"date":"2020-07-01T18:47:40","date_gmt":"2020-07-01T17:47:40","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=2999"},"modified":"2022-02-17T07:13:47","modified_gmt":"2022-02-17T07:13:47","slug":"lock-row-for-modifying","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/mysql\/examples-mysql\/lock-row-for-modifying","title":{"rendered":"Lock row for modifying"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Simple Read\/Modify\/Write lock using a DateTime column<\/h4>\n\n\n\n<p>ModifyLocked is our lock column, defined as datetime, default Null.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  \/\/----- TRY TO GET MODIFY LOCK ON THIS RECORDS ROW -----\n  \/\/Ensure the row exists (create new row if not previously stored)\n  $stmt = $maindb->prepare(\"\n    INSERT IGNORE INTO my_table\n    (\n      MyUniqueColumn,\n      ModifyLocked\n    ) VALUES (\n      ?,\n      NULL\n    )\");\n  $stmt->bind_param(\"i\", $MyUniqueColumn);\n  $stmt->execute();\n\n  \/\/Try and lock the row\n  $stmt = $maindb->prepare(\"\n    UPDATE my_table SET\n      ModifyLocked = DATE_ADD(now(), INTERVAL 90 SECOND)\n    WHERE \n      MyUniqueColumn = ? AND\n      ( (ModifyLocked IS NULL) OR (ModifyLocked &lt; DATE_SUB(now(), INTERVAL 90 SECOND)) )\n  \");\n  $stmt->bind_param(\"i\", $MyUniqueColumn);\n  $stmt->execute();\n  if ($stmt->affected_rows &lt;= 0)\n  {\n    \/\/---- ROW IS LOCKED FOR MODIFYING BY SOME OTHER PROCESS -----\n    \n    \/\/Can't access it\n    \n  }\n  else\n  {\n    \/\/----- WE HAVE LOCKED THE ROW READY FOR US TO MODIFY -----\n    $Locked = 'Y';\n\n\n    \/\/Change values as needed here\n\n\n    \/\/Write the row and remove the lock\n    $stmt = $maindb->prepare(\"\n      UPDATE my_table SET\n        ModifyLocked = NULL\n      WHERE \n        MyUniqueColumn = ?\n    \");\n    $stmt->bind_param(\"i\", $MyUniqueColumn);\n    $stmt->execute();\n  }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Simple Read\/Modify\/Write lock using a DateTime column ModifyLocked is our lock column, defined as datetime, default Null.<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[148],"tags":[],"class_list":["post-2999","post","type-post","status-publish","format-standard","hentry","category-examples-mysql"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2999","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=2999"}],"version-history":[{"count":1,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2999\/revisions"}],"predecessor-version":[{"id":3000,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2999\/revisions\/3000"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=2999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=2999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=2999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}