{"id":2041,"date":"2012-07-02T15:11:02","date_gmt":"2012-07-02T15:11:02","guid":{"rendered":"http:\/\/blog.esds.co.in\/?p=2041"},"modified":"2021-03-03T11:27:42","modified_gmt":"2021-03-03T11:27:42","slug":"php-data-objects","status":"publish","type":"post","link":"https:\/\/www.esds.co.in\/blog\/php-data-objects\/","title":{"rendered":"What is PHP Data Objects and how do I use PHP Data Objects?"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"alignright\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"421\" src=\"http:\/\/blog.esds.co.in\/wp-content\/uploads\/2012\/06\/php.png\" alt=\"\" class=\"wp-image-2043\" title=\"php\" srcset=\"https:\/\/www.esds.co.in\/blog\/wp-content\/uploads\/2012\/06\/php.png 800w, https:\/\/www.esds.co.in\/blog\/wp-content\/uploads\/2012\/06\/php-300x157.png 300w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure><\/div>\n\n\n\n<h1 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_are_PHP_Data_Objects\"><\/span>What are PHP Data Objects?<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<p style=\"text-align: justify;\">PHP recommends instead of using the standard mysql_connect() and mysql_query() functions that instead you use <strong>PHP Data Objects<\/strong>, or mysqli_() functions for database interactions. <strong>PHP Data Objects<\/strong> are a robust, easy-to-learn and easy-to-use object-oriented interface for interacting with databases. <strong>Object-oriented programming<\/strong> is a conceptual programming model for developing applications, and results in cleaner, more organised and structured code. I would assume you have some familiarity with object-oriented programming already, as it would help to understand how to use PHP Data Objects proficiently.<\/p><div id=\"ez-toc-container\" class=\"ez-toc-v2_0_76 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.esds.co.in\/blog\/php-data-objects\/#What_are_PHP_Data_Objects\" >What are PHP Data Objects?<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.esds.co.in\/blog\/php-data-objects\/#Executing_database_queries\" >Executing database queries.<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n\n\n\n<p style=\"text-align: justify;\">Connecting to a database using PHP Data Objects is incredibly simple.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>$string = &#8220;mysql:dbname=db_name;host=localhost&#8221;;<br>$user = &#8216;db_user&#8217;;<br>$password = &#8220;your password here&#8221;;<br>try<br>{<br>$db = new PDO($string, $user, $password);<br>}<br>catch (PDOException $e) {<br>echo&nbsp;&#8220;Connection failed: &#8221; . $e-&gt;getMessage();<br>die();<br>}<\/p><\/blockquote>\n\n\n\n<p style=\"text-align: justify;\">The <strong>Try, Catch<\/strong> block is used to try a block of code and if any problems occur, the errors are returned in the <strong>catch<\/strong> block. In this instance, the PDO() object would return an PDOException() object if an error occurred with the connection to the database. The getMessage() method of the PDOException class would return the error in question. It&#8217;s a super efficient way of handling errors that may otherwise cause errors to be displayed on the page.<\/p>\n\n\n\n<p style=\"text-align: justify;\">The $string variable contains the DSN (Data Source Name) which essentially is to tell PHP Data Objects what database system you want to connect to and the name of the database and where the database is located (in this instance, the database is located on the same server as the PHP application being executed on &#8211; hence <em>localhost<\/em>). For more information on DSN, see the PHP documentation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Executing_database_queries\"><\/span>Executing database queries.<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p style=\"text-align: justify;\">To execute SQL queries, here&#8217;s an example:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>$query = $db-&gt;query(&#8220;SELECT * FROM users&#8221;);<br>foreach($query as $row)<br>{<br>echo $row[&#8216;column&#8217;];<br>}<\/p><\/blockquote>\n\n\n\n<p style=\"text-align: justify;\">This will obviously loop through each result row and return the column named&nbsp;<em>column<\/em> as specified in the array index name of $row. Essentially, $query is returning an associative array that you loop through using a foreach() loop.<\/p>\n\n\n\n<p style=\"text-align: justify;\">Some more examples include:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>$query = $db-&gt;exec(&#8220;UPDATE users SET username = &#8216;$username&#8217; WHERE username = &#8216;$username_old'&#8221;); \/\/ returns number of affected rows<\/p><p>if($query)<br>{<br>echo &#8220;Row updated&#8221;;<br>}<\/p><\/blockquote>\n\n\n\n<p style=\"text-align: justify;\">Note: The exec() method does not work for SELECT queries. The exec() method executes an SQL query and returns the number of affected rows. A SELECT query does not affect any existing rows, which is why a SELECT statement doesn&#8217;t work. Do check how many rows are&nbsp;<em>returned<\/em> from a SELECT query, you can do this:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>$query = $db-&gt;query(&#8220;SELECT * FROM users&#8221;);<br>echo $query-&gt;rowCount();<\/p><\/blockquote>\n\n\n\n<p style=\"text-align: justify;\">In this instance, the method rowCount() is used to count the amount of rows returned by the query. You may notice that the query() method will return an entire object (specifically a PDOStatement object) and in the PDOStatement class there is a method called rowCount() which returns an integer value representing the amount of rows returned by the query in question. However, you can also loop through the returned results of the query() method using a foreach loop, or perhaps the first row result using the fetch() method of the PDOStatement class, which returns the next row from the result set (so in this instance it will be the first row).<\/p>\n\n\n\n<p style=\"text-align: justify;\">Unlike the PEAR library which you need to install separately on your ESDS Dedicated Server (the PEAR library has its own class for database interaction), the PDO extension is enabled by default as of PHP 5.1.0 and above.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What are PHP Data Objects? PHP recommends instead of using the standard mysql_connect() and mysql_query() functions that instead you use PHP Data Objects, or mysqli_() functions for database interactions. PHP Data Objects are a robust, easy-to-learn and easy-to-use object-oriented interface for interacting with databases. Object-oriented programming is a conceptual programming model for developing applications, and&#8230; <\/p>\n<div class=\"clear\"><\/div>\n<p><a href=\"https:\/\/www.esds.co.in\/blog\/php-data-objects\/\" class=\"gdlr-button small excerpt-read-more\">Read More<\/a><\/p>\n","protected":false},"author":8,"featured_media":2043,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[187],"tags":[660,2621,656,659,657,157,2620,655,156,654,658],"class_list":["post-2041","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-general","tag-data","tag-data-source-name","tag-database","tag-dsn","tag-mssql","tag-mysql","tag-object-oriented-programming","tag-pdo","tag-php","tag-php-data-objects","tag-sql"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.esds.co.in\/blog\/wp-json\/wp\/v2\/posts\/2041","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.esds.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.esds.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.esds.co.in\/blog\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/www.esds.co.in\/blog\/wp-json\/wp\/v2\/comments?post=2041"}],"version-history":[{"count":17,"href":"https:\/\/www.esds.co.in\/blog\/wp-json\/wp\/v2\/posts\/2041\/revisions"}],"predecessor-version":[{"id":11867,"href":"https:\/\/www.esds.co.in\/blog\/wp-json\/wp\/v2\/posts\/2041\/revisions\/11867"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.esds.co.in\/blog\/wp-json\/wp\/v2\/media\/2043"}],"wp:attachment":[{"href":"https:\/\/www.esds.co.in\/blog\/wp-json\/wp\/v2\/media?parent=2041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.esds.co.in\/blog\/wp-json\/wp\/v2\/categories?post=2041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.esds.co.in\/blog\/wp-json\/wp\/v2\/tags?post=2041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}