{"id":1361,"date":"2021-04-23T03:36:49","date_gmt":"2021-04-23T03:36:49","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=1361"},"modified":"2021-07-08T04:23:59","modified_gmt":"2021-07-08T04:23:59","slug":"php-pdo-transaction","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-pdo\/php-pdo-transaction\/","title":{"rendered":"PHP PDO Transaction"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to perform a database transaction from PHP by using PDO API.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-php-pdo-transaction'>Introduction to PHP PDO transaction <a href=\"#introduction-to-php-pdo-transaction\" class=\"anchor\" id=\"introduction-to-php-pdo-transaction\" title=\"Anchor for Introduction to PHP PDO transaction\">#<\/a><\/h2>\n\n\n\n<p>To start a transaction in PDO, you use the <code>PDO::beginTransaction()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$pdo-&gt;beginTransaction();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><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>The <code>beginTransaction()<\/code> method turns off the autocommit mode. It means that the changes made to the database via the PDO object won&#8217;t take effect until you call the <code>PDO::commit()<\/code> method.<\/p>\n\n\n\n<p>To commit a transaction, you call the <code>PDO::commit()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$pdo-&gt;commit();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><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>If you want to roll back the transaction, you can call the <code>PDO::rollback()<\/code> method:<\/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\">$pdo-&gt;rollback();<\/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>The <code>PDO::rollback()<\/code> method rolls back all changes made to the database. Also, it returns the connection to the autocommit mode.<\/p>\n\n\n\n<p>The <code>PDO::beginTransaction()<\/code> method throws an exception if the database doesn&#8217;t support transactions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='pdo-transaction-example'>PDO transaction example <a href=\"#pdo-transaction-example\" class=\"anchor\" id=\"pdo-transaction-example\" title=\"Anchor for PDO transaction example\">#<\/a><\/h2>\n\n\n\n<p>Suppose that you need to insert data into three tables: <code>books<\/code>, <code>authors<\/code>, and <code>book_authors<\/code>. <\/p>\n\n\n\n<p>To do that, you need to:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Get the author id if the author exists; otherwise, insert the author into the <code>authors<\/code> table.<\/li><li>Insert the book into the <code>books<\/code> table.<\/li><li>Insert the link between book and author into the <code>book_authors<\/code> table.<\/li><\/ul>\n\n\n\n<p>To organize the code, we&#8217;ll place all functions in the <code>functions.php<\/code> file and include it in the mains script.<\/p>\n\n\n\n<p>The following <code>get_author_id()<\/code> function finds author by first name and last name and returns the author id if the author exists in the <code>authors<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">get_author_id<\/span><span class=\"hljs-params\">(\\PDO $pdo, string $first_name, string $last_name)<\/span>\n<\/span>{\n\t$sql = <span class=\"hljs-string\">'SELECT author_id \n            FROM authors \n            WHERE first_name = :first_name \n                AND last_name = :last_name'<\/span>;\n\n\t$statement = $pdo-&gt;prepare($sql);\n\n\t$statement-&gt;bindParam(<span class=\"hljs-string\">':first_name'<\/span>, $first_name, PDO::PARAM_STR);\n\t$statement-&gt;bindParam(<span class=\"hljs-string\">':last_name'<\/span>, $last_name, PDO::PARAM_STR);\n\n\t<span class=\"hljs-keyword\">if<\/span> ($statement-&gt;execute()) {\n\t\t$row = $statement-&gt;fetch(PDO::FETCH_ASSOC);\n\t\t<span class=\"hljs-keyword\">return<\/span> $row !== <span class=\"hljs-keyword\">false<\/span> ? $row&#91;<span class=\"hljs-string\">'author_id'<\/span>] : <span class=\"hljs-keyword\">false<\/span>;\n\t}\n\n\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">false<\/span>;\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following <code>insert_author()<\/code> function inserts a new author into the <code>authors<\/code> table and returns the author id:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">insert_author<\/span><span class=\"hljs-params\">(\\PDO $pdo, string $first_name, string $last_name)<\/span>: <span class=\"hljs-title\">int<\/span>\n<\/span>{\n\t$sql = <span class=\"hljs-string\">'INSERT INTO authors(first_name, last_name) \n            VALUES(:first_name, :last_name)'<\/span>;\n\n\t$statement = $pdo-&gt;prepare($sql);\n\n\t$statement-&gt;bindParam(<span class=\"hljs-string\">':first_name'<\/span>, $first_name, PDO::PARAM_STR);\n\t$statement-&gt;bindParam(<span class=\"hljs-string\">':last_name'<\/span>, $last_name, PDO::PARAM_STR);\n\n\t$statement-&gt;execute();\n\n\t<span class=\"hljs-keyword\">return<\/span>  $pdo-&gt;lastInsertId();\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following <code>insert_book()<\/code> function inserts a new book into the <code>books<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">insert_book<\/span><span class=\"hljs-params\">(\\PDO $pdo, string $title, string $isbn, string $published_date, int $publisher_id)<\/span>: <span class=\"hljs-title\">int<\/span>\n<\/span>{\n\t$sql = <span class=\"hljs-string\">'INSERT INTO books(title, isbn, published_date, publisher_id) \n            VALUES(:title, :isbn, :published_date, :publisher_id)'<\/span>;\n\n\t$statement = $pdo-&gt;prepare($sql);\n\n\t$statement-&gt;bindParam(<span class=\"hljs-string\">':title'<\/span>, $title, PDO::PARAM_STR);\n\t$statement-&gt;bindParam(<span class=\"hljs-string\">':isbn'<\/span>, $isbn, PDO::PARAM_STR);\n\t$statement-&gt;bindParam(<span class=\"hljs-string\">':published_date'<\/span>, $published_date, PDO::PARAM_STR);\n\t$statement-&gt;bindParam(<span class=\"hljs-string\">':publisher_id'<\/span>, $publisher_id, PDO::PARAM_INT);\n\n\t$statement-&gt;execute();\n\n\t<span class=\"hljs-keyword\">return<\/span>  $pdo-&gt;lastInsertId();\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following function inserts a new row into the <code>book_authors<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">insert_book_author<\/span><span class=\"hljs-params\">(\\PDO $pdo, int $book_id, int $author_id)<\/span>\n<\/span>{\n\t$sql = <span class=\"hljs-string\">'INSERT INTO book_authors(book_id, author_id) \n            VALUES(:book_id, :author_id)'<\/span>;\n\n\t$statement = $pdo-&gt;prepare($sql);\n\n\t$statement-&gt;bindParam(<span class=\"hljs-string\">':book_id'<\/span>, $book_id, PDO::PARAM_INT);\n\t$statement-&gt;bindParam(<span class=\"hljs-string\">':author_id'<\/span>, $author_id, PDO::PARAM_INT);\n\n\t$statement-&gt;execute();\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following script uses those functions above to perform a transaction:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-string\">'functions.php'<\/span>;\n$pdo = <span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-string\">'connect.php'<\/span>;\n\n$book = &#91;\n\t<span class=\"hljs-string\">'title'<\/span> =&gt; <span class=\"hljs-string\">'Eternal'<\/span>,\n\t<span class=\"hljs-string\">'isbn'<\/span> =&gt; <span class=\"hljs-string\">'9780525539766'<\/span>,\n\t<span class=\"hljs-string\">'published_date'<\/span> =&gt; <span class=\"hljs-string\">'2021-03-23'<\/span>,\n\t<span class=\"hljs-string\">'publisher_id'<\/span> =&gt; <span class=\"hljs-number\">2<\/span>,\n];\n\n$author = &#91;\n\t<span class=\"hljs-string\">'first_name'<\/span> =&gt; <span class=\"hljs-string\">'Lisa'<\/span>,\n\t<span class=\"hljs-string\">'last_name'<\/span> =&gt; <span class=\"hljs-string\">'Scottoline'<\/span>,\n];\n\n<span class=\"hljs-keyword\">try<\/span> {\n\t$pdo-&gt;beginTransaction();\n\n\t<span class=\"hljs-comment\">\/\/ find the author by first name and last name<\/span>\n\t$author_id = get_author_id(\n\t\t$pdo,\n\t\t$author&#91;<span class=\"hljs-string\">'first_name'<\/span>],\n\t\t$author&#91;<span class=\"hljs-string\">'last_name'<\/span>]\n\t);\n\n\t<span class=\"hljs-comment\">\/\/ if author not found, insert a new author<\/span>\n\t<span class=\"hljs-keyword\">if<\/span> (!$author_id) {\n\t\t$author_id = insert_author(\n\t\t\t$pdo,\n\t\t\t$author&#91;<span class=\"hljs-string\">'first_name'<\/span>],\n\t\t\t$author&#91;<span class=\"hljs-string\">'last_name'<\/span>]\n\t\t);\n\t}\n\n\t$book_id = insert_book(\n\t\t$pdo,\n\t\t$book&#91;<span class=\"hljs-string\">'title'<\/span>],\n\t\t$book&#91;<span class=\"hljs-string\">'isbn'<\/span>],\n\t\t$book&#91;<span class=\"hljs-string\">'published_date'<\/span>],\n\t\t$book&#91;<span class=\"hljs-string\">'publisher_id'<\/span>]\n\t);\n\n\t<span class=\"hljs-comment\">\/\/ insert the link between book and author<\/span>\n\tinsert_book_author($pdo, $book_id, $author_id);\n\n\t<span class=\"hljs-comment\">\/\/ commit the transaction<\/span>\n\t$pdo-&gt;commit();\n} <span class=\"hljs-keyword\">catch<\/span> (\\PDOException $e) {\n\t<span class=\"hljs-comment\">\/\/ rollback the transaction<\/span>\n\t$pdo-&gt;rollBack();\n\n\t<span class=\"hljs-comment\">\/\/ show the error message<\/span>\n\t<span class=\"hljs-keyword\">die<\/span>($e-&gt;getMessage());\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the <code>PDO::beginTransaction()<\/code> method to start a new transaction.<\/li><li>Use the <code>PDO::commit()<\/code> method to commit a transaction and <code>PDO::rollback()<\/code> to roll back a transaction.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1361\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-pdo\/php-pdo-transaction\/\"\n\t\t\t\tdata-post-title=\"PHP PDO 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=\"1361\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-pdo\/php-pdo-transaction\/\"\n\t\t\t\tdata-post-title=\"PHP PDO 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 perform a database transaction from PHP by using PDO API.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":21,"menu_order":18,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1361","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=1361"}],"version-history":[{"count":4,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1361\/revisions"}],"predecessor-version":[{"id":2169,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1361\/revisions\/2169"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/21"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=1361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}