{"id":21,"date":"2021-03-07T08:11:05","date_gmt":"2021-03-07T08:11:05","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=21"},"modified":"2021-07-09T10:11:37","modified_gmt":"2021-07-09T10:11:37","slug":"php-pdo","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-pdo\/","title":{"rendered":"PHP PDO"},"content":{"rendered":"\n<p>PHP PDO is a database access layer that provides a uniform interface for working with multiple databases.<\/p>\n\n\n\n<p>PDO simplifies the common database operations including:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Creating database connections <\/li><li>Executing queries using prepared statements<\/li><li>Calling stored procedures<\/li><li>Performing transactions<\/li><li>And handling errors<\/li><\/ul>\n\n\n\n<p>PDO allows you to work with any database that has a PDO driver available. PDO relies on database-specific drivers, e.g., PDO_MYSQL for MySQL, PDO_PGSQL for PostgreSQL, PDO_OCI for Oracle database, etc., to function properly. <a href=\"https:\/\/www.php.net\/manual\/en\/pdo.drivers.php\" target=\"_blank\" rel=\"noreferrer noopener\">Here&#8217;s the complete list of PDO drivers<\/a>.<\/p>\n\n\n\n<p>Therefore, to use PDO for a specific database, you need to have a corresponding database driver available.<\/p>\n\n\n\n<p>The following diagram illustrates how PDO works:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/phptutorial.net\/wp-content\/uploads\/2021\/04\/php-pdo.svg\" alt=\"php pdo\" class=\"wp-image-869\"\/><\/figure><\/div>\n\n\n\n<p>PDO makes it easy to deploy PHP applications because it doesn&#8217;t require you to manually include any script files in your application like other libraries.<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='pdo-quick-start'>Section 1. PDO Quick Start <a href=\"#pdo-quick-start\" class=\"anchor\" id=\"pdo-quick-start\" title=\"Anchor for Section 1. PDO Quick Start\">#<\/a><\/h2>\n\n\n\n<p>This section shows you step by step how to connect to some relational database management systems, including MySQL and  PostgreSQL.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/phptutorial.net\/php-pdo\/pdo-connecting-to-mysql\/\">Connect to MySQL<\/a> &#8211; show you step-by-step on how to connect to the MySQL database.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-pdo\/pdo-connecting-to-postgresql\/\">Connect to PostgreSQL<\/a> &#8211; show you how to connect to the PostgreSQL database.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='creating-a-sample-database'>Section 2. Creating a sample database <a href=\"#creating-a-sample-database\" class=\"anchor\" id=\"creating-a-sample-database\" title=\"Anchor for Section 2. Creating a sample database\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/phptutorial.net\/php-pdo\/php-pdo-mysql\/\">Create a sample database<\/a>  &#8211; show you how to create a sample database and tables in the MySQL database server and a reusable script for connecting to the database.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-pdo\/pdo-creating-new-tables\/\">Creating new tables<\/a> &#8211; learn how to create a new table in a MySQL database from PHP using PDO.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='prepared-statements'>Section 3. Prepared Statements <a href=\"#prepared-statements\" class=\"anchor\" id=\"prepared-statements\" title=\"Anchor for Section 3. Prepared Statements\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/phptutorial.net\/php-pdo\/php-pdo-insert\/\">Prepared statement<\/a> &#8211; introduce to you the prepared statement.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-pdo\/pdo-like\/\">Execute the LIKE operator<\/a> &#8211; show you how to use a prepare statement to execute a query that contains the LIKE operator.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-pdo\/pdo-in\/\">Execute the IN operator<\/a> &#8211; learn how to use a prepared statement to execute a query that contains the IN operator.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='crud-in-pdo'>Section 4. CRUD in PDO <a href=\"#crud-in-pdo\" class=\"anchor\" id=\"crud-in-pdo\" title=\"Anchor for Section 4. CRUD in PDO\">#<\/a><\/h2>\n\n\n\n<p>This section illustrates the common database operation, including <strong>c<\/strong>reating (insert), <strong>r<\/strong>eading(select), <strong>u<\/strong>pdating, and <strong>d<\/strong>eleting data using PDO. These basic operations are often called CRUD.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/phptutorial.net\/php-pdo\/php-pdo-insert\/\">Inserting data into a table<\/a> &#8211; walk you through the steps of inserting data into a table of a database.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-pdo\/php-pdo-update\/\">Updating data in a table<\/a> &#8211; show you how to update data in a table from PHP using PDO.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-pdo\/php-pdo-select\/\">Selecting data from a table<\/a> &#8211; guide you on how to query data from a table.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-pdo\/php-pdo-delete\/\">Deleting data from a table<\/a> &#8211; show you how to delete data in the database table using PDO API.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='fetching-data'>Section 5. Fetching data <a href=\"#fetching-data\" class=\"anchor\" id=\"fetching-data\" title=\"Anchor for Section 5. Fetching data\">#<\/a><\/h2>\n\n\n\n<p>This section discusses various fetch* methods and modes in detail.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/phptutorial.net\/php-pdo\/php-fetch\/\">fetch()<\/a> &#8211; fetch a row from a result set associated with a PDOStatement object.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-pdo\/php-fetchall\/\">fetchAll()<\/a> &#8211; fetch all rows from a result set object into an array.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-pdo\/fetchcolumn\/\">fetchcolumn()<\/a> &#8211; fetch a single column from the next row in a result set.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-pdo\/fetchobject\/\">fetchObject() <\/a>&#8211; fetch the next row from a result set and returns it as an object of a class.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-pdo\/pdo-fetch_key_pair\/\">PDO::FETCH_KEY_PAIR<\/a>  &#8211; select a two-column result in an array where the first column is the key and the second column is the value<\/li><li><a href=\"https:\/\/phptutorial.net\/php-pdo\/pdo-fetch_group\/\">PDO::FETCH_GROUP<\/a> &#8211; group rows by the unique values of the first column in the result set.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-pdo\/pdo-fetch_class\/\">PDO::FETCH_CLASS<\/a> &#8211; show you how to return an instance of a class whose properties map to the column values from the database.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='calling-stored-procedures'>Section 6. Calling Stored Procedures <a href=\"#calling-stored-procedures\" class=\"anchor\" id=\"calling-stored-procedures\" title=\"Anchor for Section 6. Calling Stored Procedures\">#<\/a><\/h2>\n\n\n\n<p>This section shows you some examples of dealing with stored procedures, including:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/phptutorial.net\/php-pdo\/php-pdo-call-mysql-stored-procedure\/\">Calling a  MySQL stored procedure from PHP using PDO<\/a>  &#8211; show you how to call a MySQL stored procedure from PHP using PDO.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='managing-transaction-in-pdo'>Section 6. Managing Transaction in PDO <a href=\"#managing-transaction-in-pdo\" class=\"anchor\" id=\"managing-transaction-in-pdo\" title=\"Anchor for Section 6. Managing Transaction in PDO\">#<\/a><\/h2>\n\n\n\n<p>PDO provides you with some handy methods, including <code>beginTransaction()<\/code>, <code>commit()<\/code> and <code>rollBack()<\/code> that handles transactions effectively. This section gives you some examples of handling transactions in your web application.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/phptutorial.net\/php-pdo\/php-pdo-transaction\/\">PDO Transaction<\/a> &#8211; learn how to perform a database transaction from PHP using PDO.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id='pdo-references'>Section 7. PDO References <a href=\"#pdo-references\" class=\"anchor\" id=\"pdo-references\" title=\"Anchor for Section 7. PDO References\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"http:\/\/www.php.net\/manual\/en\/book.pdo.php\">http:\/\/www.php.net\/manual\/en\/book.pdo.php<\/a> &#8211; PHP Data Objects<\/li><\/ul>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>This PHP PDO tutorial introduces you to the PHP Data Objects extension that allows you to interact with any database systems effectively.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-21","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/21","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=21"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/21\/revisions"}],"predecessor-version":[{"id":2221,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/21\/revisions\/2221"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}