{"id":668,"date":"2012-05-13T21:36:36","date_gmt":"2012-05-13T21:36:36","guid":{"rendered":"http:\/\/www.withinweb.com\/info\/?p=668"},"modified":"2012-06-20T13:33:43","modified_gmt":"2012-06-20T13:33:43","slug":"examples-of-using-pdo-objects-in-php","status":"publish","type":"post","link":"https:\/\/www.withinweb.com\/info\/examples-of-using-pdo-objects-in-php\/","title":{"rendered":"Examples of using PDO objects in PHP"},"content":{"rendered":"<p>&lt;?php<\/p>\n<p>\/\/Example of fetching data from a database using PDO objects<\/p>\n<p># using the shortcut -&gt;query() method here since there are no variable<br \/>\n# values in the select statement.<\/p>\n<p>try {<\/p>\n<p>$dbhost = &#8220;localhost&#8221;;<br \/>\n$dbname\u00a0\u00a0 \u00a0= &#8220;users&#8221;;<br \/>\n$dbusername = &#8220;root&#8221;;<br \/>\n$dbpass = &#8220;&#8221;;<\/p>\n<p>\/\/Connect to the database<br \/>\n$dbh = new PDO(&#8220;mysql:host=&#8221; . $dbhost . &#8220;;dbname=&#8221; . $dbname, $dbusername, $dbpass);<\/p>\n<p>\/\/the sql query<br \/>\n$sql = &#8220;SELECT * FROM users&#8221;;<\/p>\n<p>\/\/statment handle<br \/>\n$sth = $dbh-&gt;query($sql);<\/p>\n<p># setting the fetch mode<br \/>\n$sth-&gt;setFetchMode(PDO::FETCH_ASSOC);<\/p>\n<p>echo(&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&lt;br\/&gt;&#8221;);<br \/>\necho(&#8220;An example of a while loop&lt;br\/&gt;&#8221;);<br \/>\nwhile($row = $sth-&gt;fetch()) {<br \/>\necho( $row[&#8220;first_name&#8221;] . &#8220;&lt;br\/&gt;&#8221; );<br \/>\n$table[] = $row;<br \/>\n}<\/p>\n<p>$dbh = null;<\/p>\n<p>}\u00a0 catch (PDOException $e) {<br \/>\nprint &#8220;Error!: &#8221; . $e-&gt;getMessage() . &#8220;&lt;br\/&gt;&#8221;;<br \/>\ndie();<br \/>\n}<\/p>\n<p>echo(&#8220;&lt;br\/&gt;&lt;br\/&gt;&#8221;);<\/p>\n<p>echo(&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&lt;br\/&gt;&#8221;);<br \/>\necho(&#8220;An example of looping around an array&lt;br\/&gt;&#8221;);<\/p>\n<p>if ($table) {\u00a0\u00a0 \u00a0\/\/Check if there are any rows to be displayed<br \/>\n\/\/Retrieve each element of the array<br \/>\nforeach($table as $d_row) {<br \/>\necho( $d_row[&#8220;first_name&#8221;] . &#8221; &#8221; . $d_row[&#8220;last_name&#8221;] . &#8220;&lt;br\/&gt;&#8221; );<br \/>\n}<br \/>\n}<\/p>\n<p>echo(&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&lt;br\/&gt;&#8221;);<br \/>\necho(&#8220;An example of printing one element from the array&lt;br\/&gt;&#8221;);<br \/>\necho($table[0][&#8220;first_name&#8221;]);<\/p>\n<p>?&gt;<\/p>\n<p>&lt;?php<\/p>\n<p>\/\/Example of fetching data from a database using PDO objects<\/p>\n<p>\/\/This uses a prepared statement using named values<\/p>\n<p>try {<\/p>\n<p>$dbhost = &#8220;localhost&#8221;;<br \/>\n$dbname\u00a0\u00a0 \u00a0= &#8220;users&#8221;;<br \/>\n$dbusername = &#8220;root&#8221;;<br \/>\n$dbpass = &#8220;&#8221;;<\/p>\n<p>$first_name = &#8220;%paul%&#8221;;<\/p>\n<p>\/\/Connect to the database<br \/>\n$dbh = new PDO(&#8220;mysql:host=&#8221; . $dbhost . &#8220;;dbname=&#8221; . $dbname, $dbusername, $dbpass);<\/p>\n<p>\/\/the sql query using a named placeholder<br \/>\n$sql = &#8220;SELECT * FROM users WHERE first_name LIKE :first_name &#8220;;<\/p>\n<p>\/\/statment handle<br \/>\n$sth = $dbh-&gt;prepare($sql);<\/p>\n<p>$sth-&gt;execute(array(&#8220;:first_name&#8221; =&gt; $first_name));<\/p>\n<p>$sth-&gt;setFetchMode(PDO::FETCH_ASSOC);<\/p>\n<p>echo(&#8220;&lt;br\/&gt;&lt;br\/&gt;&#8221;);<br \/>\necho(&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&lt;br\/&gt;&#8221;);<br \/>\necho(&#8220;An example of printing values from a select statement with parameters&lt;br\/&gt;&#8221;);<\/p>\n<p>while($row = $sth-&gt;fetch()) {<br \/>\necho( $row[&#8220;first_name&#8221;] . &#8220;&lt;br\/&gt;&#8221; );<br \/>\n$table[] = $row;<br \/>\n}<\/p>\n<p>$dbh = null;<\/p>\n<p>}\u00a0 catch (PDOException $e) {<br \/>\nprint &#8220;Error!: &#8221; . $e-&gt;getMessage() . &#8220;&lt;br\/&gt;&#8221;;<br \/>\ndie();<br \/>\n}<\/p>\n<p>?&gt;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&lt;?php \/\/Example of fetching data from a database using PDO objects # using the shortcut -&gt;query() method here since there are no variable # values in the select statement. try { $dbhost = &#8220;localhost&#8221;; $dbname\u00a0\u00a0 \u00a0= &#8220;users&#8221;; $dbusername = &#8220;root&#8221;;<span class=\"ellipsis\">&hellip;<\/span><\/p>\n<div class=\"read-more\"><a href=\"https:\/\/www.withinweb.com\/info\/examples-of-using-pdo-objects-in-php\/\">Read more <span class=\"screen-reader-text\">Examples of using PDO objects in PHP<\/span><span class=\"meta-nav\"> &#8250;<\/span><\/a><\/div>\n<p><!-- end of .read-more --><\/p>\n","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,17],"tags":[],"class_list":["post-668","post","type-post","status-publish","format-standard","hentry","category-general-php","category-pdo-objects"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Examples of using PDO objects in PHP - PHP Web Applications<\/title>\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.withinweb.com\/info\/examples-of-using-pdo-objects-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Examples of using PDO objects in PHP - PHP Web Applications\" \/>\n<meta property=\"og:description\" content=\"&lt;?php \/\/Example of fetching data from a database using PDO objects # using the shortcut -&gt;query() method here since there are no variable # values in the select statement. try { $dbhost = &#8220;localhost&#8221;; $dbname\u00a0\u00a0 \u00a0= &#8220;users&#8221;; $dbusername = &#8220;root&#8221;;&hellip;Read more Examples of using PDO objects in PHP &#8250;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.withinweb.com\/info\/examples-of-using-pdo-objects-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"PHP Web Applications\" \/>\n<meta property=\"article:published_time\" content=\"2012-05-13T21:36:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-06-20T13:33:43+00:00\" \/>\n<meta name=\"author\" content=\"paulv\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"paulv\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/examples-of-using-pdo-objects-in-php\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/examples-of-using-pdo-objects-in-php\\\/\"},\"author\":{\"name\":\"paulv\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\"},\"headline\":\"Examples of using PDO objects in PHP\",\"datePublished\":\"2012-05-13T21:36:36+00:00\",\"dateModified\":\"2012-06-20T13:33:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/examples-of-using-pdo-objects-in-php\\\/\"},\"wordCount\":346,\"commentCount\":0,\"articleSection\":[\"General PHP\",\"PDO Objects\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.withinweb.com\\\/info\\\/examples-of-using-pdo-objects-in-php\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/examples-of-using-pdo-objects-in-php\\\/\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/examples-of-using-pdo-objects-in-php\\\/\",\"name\":\"Examples of using PDO objects in PHP - PHP Web Applications\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#website\"},\"datePublished\":\"2012-05-13T21:36:36+00:00\",\"dateModified\":\"2012-06-20T13:33:43+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/examples-of-using-pdo-objects-in-php\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.withinweb.com\\\/info\\\/examples-of-using-pdo-objects-in-php\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/examples-of-using-pdo-objects-in-php\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Examples of using PDO objects in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#website\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/\",\"name\":\"PHP Web Applications\",\"description\":\"Information and support for products of WithinWeb.com\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\",\"name\":\"paulv\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/author\\\/paulv\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Examples of using PDO objects in PHP - PHP Web Applications","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.withinweb.com\/info\/examples-of-using-pdo-objects-in-php\/","og_locale":"en_US","og_type":"article","og_title":"Examples of using PDO objects in PHP - PHP Web Applications","og_description":"&lt;?php \/\/Example of fetching data from a database using PDO objects # using the shortcut -&gt;query() method here since there are no variable # values in the select statement. try { $dbhost = &#8220;localhost&#8221;; $dbname\u00a0\u00a0 \u00a0= &#8220;users&#8221;; $dbusername = &#8220;root&#8221;;&hellip;Read more Examples of using PDO objects in PHP &#8250;","og_url":"https:\/\/www.withinweb.com\/info\/examples-of-using-pdo-objects-in-php\/","og_site_name":"PHP Web Applications","article_published_time":"2012-05-13T21:36:36+00:00","article_modified_time":"2012-06-20T13:33:43+00:00","author":"paulv","twitter_card":"summary_large_image","twitter_misc":{"Written by":"paulv","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.withinweb.com\/info\/examples-of-using-pdo-objects-in-php\/#article","isPartOf":{"@id":"https:\/\/www.withinweb.com\/info\/examples-of-using-pdo-objects-in-php\/"},"author":{"name":"paulv","@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c"},"headline":"Examples of using PDO objects in PHP","datePublished":"2012-05-13T21:36:36+00:00","dateModified":"2012-06-20T13:33:43+00:00","mainEntityOfPage":{"@id":"https:\/\/www.withinweb.com\/info\/examples-of-using-pdo-objects-in-php\/"},"wordCount":346,"commentCount":0,"articleSection":["General PHP","PDO Objects"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.withinweb.com\/info\/examples-of-using-pdo-objects-in-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.withinweb.com\/info\/examples-of-using-pdo-objects-in-php\/","url":"https:\/\/www.withinweb.com\/info\/examples-of-using-pdo-objects-in-php\/","name":"Examples of using PDO objects in PHP - PHP Web Applications","isPartOf":{"@id":"https:\/\/www.withinweb.com\/info\/#website"},"datePublished":"2012-05-13T21:36:36+00:00","dateModified":"2012-06-20T13:33:43+00:00","author":{"@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c"},"breadcrumb":{"@id":"https:\/\/www.withinweb.com\/info\/examples-of-using-pdo-objects-in-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.withinweb.com\/info\/examples-of-using-pdo-objects-in-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.withinweb.com\/info\/examples-of-using-pdo-objects-in-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.withinweb.com\/info\/"},{"@type":"ListItem","position":2,"name":"Examples of using PDO objects in PHP"}]},{"@type":"WebSite","@id":"https:\/\/www.withinweb.com\/info\/#website","url":"https:\/\/www.withinweb.com\/info\/","name":"PHP Web Applications","description":"Information and support for products of WithinWeb.com","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.withinweb.com\/info\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c","name":"paulv","url":"https:\/\/www.withinweb.com\/info\/author\/paulv\/"}]}},"_links":{"self":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/668","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/comments?post=668"}],"version-history":[{"count":4,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/668\/revisions"}],"predecessor-version":[{"id":787,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/668\/revisions\/787"}],"wp:attachment":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/media?parent=668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/categories?post=668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/tags?post=668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}