{"id":339,"date":"2014-08-19T22:33:21","date_gmt":"2014-08-20T06:33:21","guid":{"rendered":"http:\/\/www.tech.dimprash.com\/?p=339"},"modified":"2014-09-24T15:40:33","modified_gmt":"2014-09-24T23:40:33","slug":"php-data-objects","status":"publish","type":"post","link":"http:\/\/www.tech.dimprash.com\/?p=339","title":{"rendered":"PHP Data Objects &#038; Prepared Statements"},"content":{"rendered":"<p>PDO provides a data-access abstraction layer, which means that, regardless of which database you&#8217;re using, you use the same functions to issue queries and fetch data.<\/p>\n<p><code>try {<br \/>\n    $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);<br \/>\n    foreach($dbh->query('SELECT * from FOO') as $row) {<br \/>\n        print_r($row);<br \/>\n    }<br \/>\n    $dbh = null;<br \/>\n} catch (PDOException $e) {<br \/>\n    print \"Error!: \" . $e->getMessage() . \"<br \/>\";<br \/>\n    die();<br \/>\n}<\/p>\n<p>\/\/ Persistent Connections<br \/>\n<?php\n$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(\n    PDO::ATTR_PERSISTENT => true<br \/>\n));<br \/>\n?><\/code><\/p>\n<p><strong>Prepared Statements :<\/strong><br \/>\n They can be thought of as a kind of compiled template for the SQL that an application wants to run, that can be customized using variable parameters. Prepared statements offer two major benefits:<br \/>\na) The query only needs to be parsed (or prepared) once, but can be executed multiple times with the same or different parameters.<br \/>\nb) The parameters to prepared statements don&#8217;t need to be quoted; the driver automatically handles this. If an application exclusively uses prepared statements, the developer can be sure that no SQL injection will occur <\/p>\n<p><code>$stmt = $dbh->prepare(\"INSERT INTO REGISTRY (name, value) VALUES (:name, :value)\");<br \/>\n$stmt->bindParam(':name', $name);<br \/>\n$stmt->bindParam(':value', $value);<\/p>\n<p>\/\/ insert one row<br \/>\n$name = 'one';<br \/>\n$value = 1;<br \/>\n$stmt->execute();<\/code><\/p>\n<p>\/\/ repeated inserts<br \/>\n<code>$stmt = $dbh->prepare(\"INSERT INTO REGISTRY (name, value) VALUES (?, ?)\");<br \/>\n$stmt->bindParam(1, $name);<br \/>\n$stmt->bindParam(2, $value);<\/code><\/p>\n<p>\/\/ fetching data<br \/>\n<code>$stmt = $dbh->prepare(\"SELECT * FROM REGISTRY where name = ?\");<br \/>\nif ($stmt->execute(array($_GET['name']))) {<br \/>\n  while ($row = $stmt->fetch()) {<br \/>\n    print_r($row);<br \/>\n  }<br \/>\n}<\/code><\/p>\n<p><strong>PDO vs mysqli :<\/strong><br \/>\nWhile PDO has its advantages, such as a clean, simple, portable API, its main disadvantage is that it doesn&#8217;t allow you to use all of the advanced features that are available in the latest versions of MySQL server. For example, PDO does not allow you to use MySQL&#8217;s support for Multiple Statements.<\/p>\n<p>PDO works with several databases whereas MySQLi works with MySQL only. <\/p>\n<p>PDO has named parameters whereas MySQLi does not.<br \/>\n$pdo->prepare(&#8216;SELECT * FROM users WHERE username = :username AND email = :email&#8217;);<br \/>\n$params = array(&#8216;:username&#8217; => &#8216;test&#8217;, &#8216;:email&#8217; => $mail);<\/p>\n<p>$mysqli->prepare(&#8216;SELECT * FROM users WHERE username = ? AND email = ?&#8217;);<br \/>\n$query->bind_param(&#8216;test&#8217;, $mail);<\/p>\n<p><a href=\"http:\/\/code.tutsplus.com\/tutorials\/pdo-vs-mysqli-which-should-you-use--net-24059\">http:\/\/code.tutsplus.com\/tutorials\/pdo-vs-mysqli-which-should-you-use&#8211;net-24059<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PDO provides a data-access abstraction layer, which means that, regardless of which database you&#8217;re using, you use the same functions to issue queries and fetch data. try { $dbh = new PDO(&#8216;mysql:host=localhost;dbname=test&#8217;, $user, $pass); foreach($dbh->query(&#8216;SELECT * from FOO&#8217;) as $row) { print_r($row); } $dbh = null; } catch (PDOException $e) { print &#8220;Error!: &#8221; . &hellip; <a href=\"http:\/\/www.tech.dimprash.com\/?p=339\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">PHP Data Objects &#038; Prepared Statements<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-339","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/339","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=339"}],"version-history":[{"count":9,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/339\/revisions"}],"predecessor-version":[{"id":341,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/339\/revisions\/341"}],"wp:attachment":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=339"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}