{"id":712,"date":"2012-05-27T18:57:31","date_gmt":"2012-05-27T18:57:31","guid":{"rendered":"http:\/\/www.withinweb.com\/info\/?p=712"},"modified":"2012-09-20T08:15:45","modified_gmt":"2012-09-20T08:15:45","slug":"basic-syntax-of-pdo-objects-for-php-5-1-and-above","status":"publish","type":"post","link":"https:\/\/www.withinweb.com\/info\/basic-syntax-of-pdo-objects-for-php-5-1-and-above\/","title":{"rendered":"Basic syntax of PDO Objects for PHP 5.1 and above"},"content":{"rendered":"<p>Here are some of the basic syntax for using PDO Objects.<\/p>\n<p>The advantage of PDO objects is that you pass your variables into the SQL function using <strong>prepared statements<\/strong>.<\/p>\n<p>Prepared statements are what are termed <strong>paramatised queries<\/strong> when working with program languages like Microsoft dot.net and<br \/>\nprovide a way to prevents sql injection into databases.<\/p>\n<blockquote><p>SQL FETCH<br \/>\n&lt;?php<br \/>\n$dbh = new PDO(&#8216;mysql:host=localhost;dbname=test&#8217;, $user, $pass);<br \/>\n$stmt = $dbh-&gt;prepare(&#8220;SELECT * FROM REGISTRY where name = ?&#8221;);<br \/>\nif ($stmt-&gt;execute(array($_GET[&#8216;name&#8217;]))) {<br \/>\nwhile ($row = $stmt-&gt;fetch()) {<br \/>\nprint_r($row);<br \/>\n}<br \/>\n}<br \/>\n?&gt;<\/p><\/blockquote>\n<blockquote><p>SQL MODIFY<br \/>\n&lt;?php<br \/>\n$dbh = new PDO(&#8216;mysql:host=localhost;dbname=test&#8217;, $user, $pass);<br \/>\n$stmt = $dbh-&gt;prepare(&#8220;INSERT INTO REGISTRY (name, value) VALUES (?, ?)&#8221;);<br \/>\n$stmt-&gt;bindParam(1, $name);<br \/>\n$stmt-&gt;bindParam(2, $value);<\/p>\n<p>\/\/ insert one row<br \/>\n$name = &#8216;one&#8217;;<br \/>\n$value = 1;<br \/>\n$stmt-&gt;execute();<\/p>\n<p>\/\/ insert another row with different values<br \/>\n$name = &#8216;two&#8217;;<br \/>\n$value = 2;<br \/>\n$stmt-&gt;execute();<br \/>\n?&gt;<\/p><\/blockquote>\n<p>It is advisable to use try \/ catch statements around PDO or and print out friendly error messages or otherwise it is<br \/>\npossible that an error will display internal details that you don&#8217;t want users to see.<\/p>\n<blockquote><p>&lt;?php<br \/>\ntry {<br \/>\n$dbh = new PDO(&#8216;mysql:host=localhost;dbname=test&#8217;, $user, $pass);<br \/>\nforeach($dbh-&gt;query(&#8216;SELECT * from table&#8217;) as $row) {<br \/>\nprint_r($row);<br \/>\n}<br \/>\n$dbh = null;<br \/>\n} catch (PDOException $e) {<br \/>\nprint &#8220;Error!: &#8221; . $e-&gt;getMessage() . &#8220;&lt;br\/&gt;&#8221;;<br \/>\ndie();<br \/>\n}<br \/>\n?&gt;<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Here are some of the basic syntax for using PDO Objects. The advantage of PDO objects is that you pass your variables into the SQL function using prepared statements. Prepared statements are what are termed paramatised queries when working with<span class=\"ellipsis\">&hellip;<\/span><\/p>\n<div class=\"read-more\"><a href=\"https:\/\/www.withinweb.com\/info\/basic-syntax-of-pdo-objects-for-php-5-1-and-above\/\">Read more <span class=\"screen-reader-text\">Basic syntax of PDO Objects for PHP 5.1 and above<\/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-712","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>Basic syntax of PDO Objects for PHP 5.1 and above - 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\/basic-syntax-of-pdo-objects-for-php-5-1-and-above\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Basic syntax of PDO Objects for PHP 5.1 and above - PHP Web Applications\" \/>\n<meta property=\"og:description\" content=\"Here are some of the basic syntax for using PDO Objects. The advantage of PDO objects is that you pass your variables into the SQL function using prepared statements. Prepared statements are what are termed paramatised queries when working with&hellip;Read more Basic syntax of PDO Objects for PHP 5.1 and above &#8250;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.withinweb.com\/info\/basic-syntax-of-pdo-objects-for-php-5-1-and-above\/\" \/>\n<meta property=\"og:site_name\" content=\"PHP Web Applications\" \/>\n<meta property=\"article:published_time\" content=\"2012-05-27T18:57:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2012-09-20T08:15:45+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=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/basic-syntax-of-pdo-objects-for-php-5-1-and-above\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/basic-syntax-of-pdo-objects-for-php-5-1-and-above\\\/\"},\"author\":{\"name\":\"paulv\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\"},\"headline\":\"Basic syntax of PDO Objects for PHP 5.1 and above\",\"datePublished\":\"2012-05-27T18:57:31+00:00\",\"dateModified\":\"2012-09-20T08:15:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/basic-syntax-of-pdo-objects-for-php-5-1-and-above\\\/\"},\"wordCount\":235,\"commentCount\":0,\"articleSection\":[\"General PHP\",\"PDO Objects\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.withinweb.com\\\/info\\\/basic-syntax-of-pdo-objects-for-php-5-1-and-above\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/basic-syntax-of-pdo-objects-for-php-5-1-and-above\\\/\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/basic-syntax-of-pdo-objects-for-php-5-1-and-above\\\/\",\"name\":\"Basic syntax of PDO Objects for PHP 5.1 and above - PHP Web Applications\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#website\"},\"datePublished\":\"2012-05-27T18:57:31+00:00\",\"dateModified\":\"2012-09-20T08:15:45+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/basic-syntax-of-pdo-objects-for-php-5-1-and-above\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.withinweb.com\\\/info\\\/basic-syntax-of-pdo-objects-for-php-5-1-and-above\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/basic-syntax-of-pdo-objects-for-php-5-1-and-above\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Basic syntax of PDO Objects for PHP 5.1 and above\"}]},{\"@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":"Basic syntax of PDO Objects for PHP 5.1 and above - 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\/basic-syntax-of-pdo-objects-for-php-5-1-and-above\/","og_locale":"en_US","og_type":"article","og_title":"Basic syntax of PDO Objects for PHP 5.1 and above - PHP Web Applications","og_description":"Here are some of the basic syntax for using PDO Objects. The advantage of PDO objects is that you pass your variables into the SQL function using prepared statements. Prepared statements are what are termed paramatised queries when working with&hellip;Read more Basic syntax of PDO Objects for PHP 5.1 and above &#8250;","og_url":"https:\/\/www.withinweb.com\/info\/basic-syntax-of-pdo-objects-for-php-5-1-and-above\/","og_site_name":"PHP Web Applications","article_published_time":"2012-05-27T18:57:31+00:00","article_modified_time":"2012-09-20T08:15:45+00:00","author":"paulv","twitter_card":"summary_large_image","twitter_misc":{"Written by":"paulv","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.withinweb.com\/info\/basic-syntax-of-pdo-objects-for-php-5-1-and-above\/#article","isPartOf":{"@id":"https:\/\/www.withinweb.com\/info\/basic-syntax-of-pdo-objects-for-php-5-1-and-above\/"},"author":{"name":"paulv","@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c"},"headline":"Basic syntax of PDO Objects for PHP 5.1 and above","datePublished":"2012-05-27T18:57:31+00:00","dateModified":"2012-09-20T08:15:45+00:00","mainEntityOfPage":{"@id":"https:\/\/www.withinweb.com\/info\/basic-syntax-of-pdo-objects-for-php-5-1-and-above\/"},"wordCount":235,"commentCount":0,"articleSection":["General PHP","PDO Objects"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.withinweb.com\/info\/basic-syntax-of-pdo-objects-for-php-5-1-and-above\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.withinweb.com\/info\/basic-syntax-of-pdo-objects-for-php-5-1-and-above\/","url":"https:\/\/www.withinweb.com\/info\/basic-syntax-of-pdo-objects-for-php-5-1-and-above\/","name":"Basic syntax of PDO Objects for PHP 5.1 and above - PHP Web Applications","isPartOf":{"@id":"https:\/\/www.withinweb.com\/info\/#website"},"datePublished":"2012-05-27T18:57:31+00:00","dateModified":"2012-09-20T08:15:45+00:00","author":{"@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c"},"breadcrumb":{"@id":"https:\/\/www.withinweb.com\/info\/basic-syntax-of-pdo-objects-for-php-5-1-and-above\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.withinweb.com\/info\/basic-syntax-of-pdo-objects-for-php-5-1-and-above\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.withinweb.com\/info\/basic-syntax-of-pdo-objects-for-php-5-1-and-above\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.withinweb.com\/info\/"},{"@type":"ListItem","position":2,"name":"Basic syntax of PDO Objects for PHP 5.1 and above"}]},{"@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\/712","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=712"}],"version-history":[{"count":6,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/712\/revisions"}],"predecessor-version":[{"id":897,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/712\/revisions\/897"}],"wp:attachment":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/media?parent=712"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/categories?post=712"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/tags?post=712"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}