{"id":2895,"date":"2013-06-09T19:17:36","date_gmt":"2013-06-10T03:17:36","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=2895"},"modified":"2024-01-14T21:29:17","modified_gmt":"2024-01-15T04:29:17","slug":"php-mysql-transaction","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-transaction\/","title":{"rendered":"PHP MySQL Transaction"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to handle MySQL transactions in PHP to ensure the data integrity of the database.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to PHP MySQL Transaction<\/h2>\n\n\n\n<p>A <a href=\"https:\/\/www.mysqltutorial.org\/mysql-stored-procedure\/mysql-transactions\/\">transaction<\/a> is a set of SQL statements that need to be executed in an all-or-nothing manner. <\/p>\n\n\n\n<p>A transaction is considered successful only if all SQL statements are executed successfully. The failure of any statement will trigger the database to roll back to the original state to prevent data inconsistency.<\/p>\n\n\n\n<p>A classic example of a transaction is a money transfer transaction from one bank account to another, involving three steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, check the balance of the sender to ensure it is sufficient for the transfer.<\/li>\n\n\n\n<li>Second, if the amount is sufficient, deduct it from the balance of the sender&#8217;s account.<\/li>\n\n\n\n<li>Third, add the amount to the balance of the receiver account.<\/li>\n<\/ul>\n\n\n\n<p>If an error occurs in the second step, the third step should not proceed. Similarly, if an error occurs in the third step, the second step must be reversed. <\/p>\n\n\n\n<p>The amounts of both accounts remain intact in case of failure or are adjusted correctly if the transaction is completed successfully.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL transaction in PHP<\/h2>\n\n\n\n<p>When you use PDO to <a title=\"PHP MySQL Connect\" href=\"https:\/\/www.mysqltutorial.org\/php-mysql\/php-connecting-to-mysql-database\/\">create a connection to the database<\/a> that supports transactions, the auto-commit mode is set. This means that every query you issue is wrapped inside an implicit transaction.<\/p>\n\n\n\n<p>Please be aware that not all <a title=\"MySQL Storage Engines\" href=\"https:\/\/www.mysqltutorial.org\/mysql-administration\/mysql-storage-engines\/\">storage engines<\/a> in MySQL support transactions. For example, MyISAM does not support transactions, while InnoDB does.<\/p>\n\n\n\n<p>To handle MySQL transactions in PHP, you follow these steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Start the transaction by calling the <code>beginTransaction()<\/code> method of the PDO object.<\/li>\n\n\n\n<li>Place the SQL statements and the&nbsp; <code>commit()<\/code> method call in a <code>try<\/code> block.<\/li>\n\n\n\n<li>In the <code>catch<\/code> block, roll back the transaction by calling the <code>rollBack()<\/code> method of the PDO object.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Setting up a sample table<\/h3>\n\n\n\n<p>We will <a title=\"MySQLCreate Table\" href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-create-table\/\">create a table<\/a> named <code>accounts<\/code> to demonstrate the money transfer between two bank accounts.<\/p>\n\n\n\n<p>First, execute the following statement to create the <code>accounts<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> accounts (\n    <span class=\"hljs-keyword\">id<\/span>     <span class=\"hljs-built_in\">INT<\/span> AUTO_INCREMENT PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n    <span class=\"hljs-keyword\">name<\/span>   <span class=\"hljs-built_in\">VARCHAR<\/span> (<span class=\"hljs-number\">50<\/span>)    <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    amount <span class=\"hljs-built_in\">DECIMAL<\/span> (<span class=\"hljs-number\">19<\/span>, <span class=\"hljs-number\">4<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, <a title=\"MySQL INSERT statement\" href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/\">insert <\/a>two rows into the <code>accounts<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> accounts(<span class=\"hljs-keyword\">name<\/span>,amount)\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'John'<\/span>,<span class=\"hljs-number\">25000<\/span>),\n      (<span class=\"hljs-string\">'Mary'<\/span>,<span class=\"hljs-number\">95000<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, <a title=\"MySQL query data\" href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-select-from\/\">query <\/a>the <code>accounts<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">SELECT * FROM accounts;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+----+------+------------+\n| id | name | amount     |\n+----+------+------------+\n|  <span class=\"hljs-number\">1<\/span> | John | <span class=\"hljs-number\">25000.0000<\/span> |\n|  <span class=\"hljs-number\">2<\/span> | Mary | <span class=\"hljs-number\">95000.0000<\/span> |\n+----+------+------------+\n<span class=\"hljs-number\">2<\/span> rows <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Handling transactions in PHP<\/h2>\n\n\n\n<p>The following script illustrates how to handle transactions in PHP:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n<span class=\"hljs-keyword\">require_once<\/span> <span class=\"hljs-string\">'config.php'<\/span>;\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">transfer<\/span><span class=\"hljs-params\">($conn, $sender, $receiver, $amount)<\/span>\n<\/span>{\n    <span class=\"hljs-keyword\">try<\/span> {\n        $conn-&gt;beginTransaction();\n\n        <span class=\"hljs-comment\">\/\/ get balance of the sender<\/span>\n        $sql = <span class=\"hljs-string\">'SELECT amount FROM accounts WHERE id=:sender'<\/span>;\n        $stmt = $conn-&gt;prepare($sql);\n        $stmt-&gt;execute(&#91;<span class=\"hljs-string\">\":sender\"<\/span> =&gt; $sender]);\n        $availableAmount = $stmt-&gt;fetchColumn();\n        $stmt-&gt;closeCursor();\n\n        <span class=\"hljs-keyword\">if<\/span> ($availableAmount &lt; $amount) {\n            <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'Insufficient amount to transfer'<\/span>;\n            <span class=\"hljs-keyword\">return<\/span> $conn-&gt;rollBack();\n        }\n\n        <span class=\"hljs-comment\">\/\/ deduct from the sender<\/span>\n        $sql_deduct = <span class=\"hljs-string\">'UPDATE accounts\n\t\t               SET amount = amount - :amount\n\t\t\t\t       WHERE id = :sender'<\/span>;\n\n        $stmt = $conn-&gt;prepare($sql_deduct);\n        $stmt-&gt;execute(&#91;<span class=\"hljs-string\">\":sender\"<\/span> =&gt; $sender, <span class=\"hljs-string\">\":amount\"<\/span> =&gt; $amount]);\n        $stmt-&gt;closeCursor();\n\n        <span class=\"hljs-comment\">\/\/ add amount to the receiver<\/span>\n        $sql_add = <span class=\"hljs-string\">'UPDATE accounts\n                    SET amount = amount + :amount\n                    WHERE id = :receiver'<\/span>;\n\n        $stmt = $conn-&gt;prepare($sql_add);\n        $stmt-&gt;execute(&#91;<span class=\"hljs-string\">\":receiver\"<\/span> =&gt; $receiver, <span class=\"hljs-string\">\":amount\"<\/span> =&gt; $amount]);\n\n        <span class=\"hljs-comment\">\/\/ commit the transaction<\/span>\n        $conn-&gt;commit();\n        <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'The amount has been transferred successfully'<\/span>;\n    } <span class=\"hljs-keyword\">catch<\/span> (PDOException $e) {\n        <span class=\"hljs-keyword\">if<\/span>($conn) {\n            $conn-&gt;rollBack();\n        }\n        <span class=\"hljs-keyword\">die<\/span>($e);\n    }\n}\n\n<span class=\"hljs-comment\">\/\/ connect the database<\/span>\n$conn = <span class=\"hljs-keyword\">new<\/span> PDO(<span class=\"hljs-string\">\"mysql:host=$host;dbname=$dbname\"<\/span>, $username, $password);\n\n<span class=\"hljs-comment\">\/\/ transfer 30K from from account 1 to 2<\/span>\ntransfer($conn, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">30000<\/span>); <span class=\"hljs-comment\">\/\/ failed<\/span>\n\n<span class=\"hljs-comment\">\/\/ transfer 5K from from account 1 to 2<\/span>\ntransfer($conn, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">5000<\/span>); <span class=\"hljs-comment\">\/\/ success<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, define a function <code>transfer()<\/code> that transfers an amount from sender to receiver.<\/p>\n\n\n\n<p>Second, start a new transaction by calling the <code>beginTransaction()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$conn-&gt;beginTransaction();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, get the sender&#8217;s balance:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">\/\/ get balance of the sender<\/span>\n$sql = <span class=\"hljs-string\">'SELECT amount FROM accounts WHERE id=:sender'<\/span>;\n$stmt = $conn-&gt;prepare($sql);\n$stmt-&gt;execute(&#91;<span class=\"hljs-string\">\":sender\"<\/span> =&gt; $sender]);\n$availableAmount = $stmt-&gt;fetchColumn();\n$stmt-&gt;closeCursor();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fourth, issue an error if the available amount is less than the amount that will be transferred:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">if<\/span> ($availableAmount &lt; $amount) {\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'Insufficient amount to transfer'<\/span>;\n    <span class=\"hljs-keyword\">return<\/span> $conn-&gt;rollBack();\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fifth, deduct from the sender&#8217;s balance:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$sql_deduct = <span class=\"hljs-string\">'UPDATE accounts\n               SET amount = amount - :amount\n\t       WHERE id = :sender'<\/span>;\n\n$stmt = $conn-&gt;prepare($sql_deduct);\n$stmt-&gt;execute(&#91;<span class=\"hljs-string\">\":sender\"<\/span> =&gt; $sender, <span class=\"hljs-string\">\":amount\"<\/span> =&gt; $amount]);\n$stmt-&gt;closeCursor();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Sixth, add the amount to the receiver&#8217;s balance:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$sql_add = <span class=\"hljs-string\">'UPDATE accounts\n            SET amount = amount + :amount\n            WHERE id = :receiver'<\/span>;\n\n$stmt = $conn-&gt;prepare($sql_add);\n$stmt-&gt;execute(&#91;<span class=\"hljs-string\">\":receiver\"<\/span> =&gt; $receiver, <span class=\"hljs-string\">\":amount\"<\/span> =&gt; $amount]);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Seventh, commit the transaction by calling the <code>commit()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$conn-&gt;commit();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Eighth, roll back the transaction (in the <code>catch<\/code> block) if any error occurs:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$conn-&gt;rollBack();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Testing<\/h3>\n\n\n\n<p>First, connect to the database:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$conn = <span class=\"hljs-keyword\">new<\/span> PDO(<span class=\"hljs-string\">\"mysql:host=$host;dbname=$dbname\"<\/span>, $username, $password);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, transfer 30K from account 1 to account 2:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$obj-&gt;transfer(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">30000<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Error:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">Insufficient amount to transfer<\/code><\/span><\/pre>\n\n\n<p>Third, make another transfer that transfers 5K from account 1 to account 2:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$obj-&gt;transfer(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">5000<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">The amount has been transferred successfully.<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>beginTransaction()<\/code> method of the PDO object to start a transaction.<\/li>\n\n\n\n<li>Call the <code>commit()<\/code> method of the PDO object to commit the changes made during the transaction to the database.<\/li>\n\n\n\n<li>Call the <code>rollback()<\/code> method of the PDO object to roll back the changes made during the transaction.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful? <\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2895\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-transaction\/\"\n\t\t\t\tdata-post-title=\"PHP MySQL Transaction\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2895\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-transaction\/\"\n\t\t\t\tdata-post-title=\"PHP MySQL Transaction\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to handle MySQL transactions in PHP to ensure the data integrity of the database.<\/p>\n","protected":false},"author":2,"featured_media":2902,"parent":1268,"menu_order":7,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-2895","page","type-page","status-publish","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PHP MySQL Transaction<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn how to handle MySQL transactions in PHP to ensure the data integrity of the database.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-transaction\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP MySQL Transaction\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn how to handle MySQL transactions in PHP to ensure the data integrity of the database.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-transaction\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-15T04:29:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/06\/PHP-MySQL-Transaction.gif\" \/>\n\t<meta property=\"og:image:width\" content=\"195\" \/>\n\t<meta property=\"og:image:height\" content=\"144\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/gif\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/php-mysql\\\/php-mysql-transaction\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/php-mysql\\\/php-mysql-transaction\\\/\",\"name\":\"PHP MySQL Transaction\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/php-mysql\\\/php-mysql-transaction\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/php-mysql\\\/php-mysql-transaction\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2013\\\/06\\\/PHP-MySQL-Transaction.gif\",\"datePublished\":\"2013-06-10T03:17:36+00:00\",\"dateModified\":\"2024-01-15T04:29:17+00:00\",\"description\":\"In this tutorial, you will learn how to handle MySQL transactions in PHP to ensure the data integrity of the database.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/php-mysql\\\/php-mysql-transaction\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/php-mysql\\\/php-mysql-transaction\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/php-mysql\\\/php-mysql-transaction\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2013\\\/06\\\/PHP-MySQL-Transaction.gif\",\"contentUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2013\\\/06\\\/PHP-MySQL-Transaction.gif\",\"width\":195,\"height\":144,\"caption\":\"PHP MySQL Transaction\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/php-mysql\\\/php-mysql-transaction\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP MySQL Tutorial\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/php-mysql\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PHP MySQL Transaction\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP MySQL Transaction","description":"In this tutorial, you will learn how to handle MySQL transactions in PHP to ensure the data integrity of the database.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-transaction\/","og_locale":"en_US","og_type":"article","og_title":"PHP MySQL Transaction","og_description":"In this tutorial, you will learn how to handle MySQL transactions in PHP to ensure the data integrity of the database.","og_url":"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-transaction\/","og_site_name":"MySQL Tutorial","article_modified_time":"2024-01-15T04:29:17+00:00","og_image":[{"width":195,"height":144,"url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/06\/PHP-MySQL-Transaction.gif","type":"image\/gif"}],"twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-transaction\/","url":"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-transaction\/","name":"PHP MySQL Transaction","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-transaction\/#primaryimage"},"image":{"@id":"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-transaction\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/06\/PHP-MySQL-Transaction.gif","datePublished":"2013-06-10T03:17:36+00:00","dateModified":"2024-01-15T04:29:17+00:00","description":"In this tutorial, you will learn how to handle MySQL transactions in PHP to ensure the data integrity of the database.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-transaction\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-transaction\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-transaction\/#primaryimage","url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/06\/PHP-MySQL-Transaction.gif","contentUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/06\/PHP-MySQL-Transaction.gif","width":195,"height":144,"caption":"PHP MySQL Transaction"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-transaction\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"PHP MySQL Tutorial","item":"https:\/\/www.mysqltutorial.org\/php-mysql\/"},{"@type":"ListItem","position":3,"name":"PHP MySQL Transaction"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/2895","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=2895"}],"version-history":[{"count":5,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/2895\/revisions"}],"predecessor-version":[{"id":14581,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/2895\/revisions\/14581"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/1268"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media\/2902"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=2895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}