{"id":2370,"date":"2014-12-22T10:24:00","date_gmt":"2014-12-22T08:24:00","guid":{"rendered":"http:\/\/dima.fi\/blog\/?p=2370"},"modified":"2026-01-10T22:59:30","modified_gmt":"2026-01-10T20:59:30","slug":"mysql-pdo-extension","status":"publish","type":"post","link":"https:\/\/dima.fi\/blog\/mysql-pdo-extension\/","title":{"rendered":"MySQL PDO extension"},"content":{"rendered":"\n<p>Have you been or are you in this situation:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em>Hmm.. PDO.. gotta do some wrapper.<\/em><\/li><li><em>My gosh, it&#8217;s ugly, gotta do some wrapper.<\/em><\/li><li><em>Wtf, PDO. Can&#8217;t learn. Gotta get some helper class.<\/em><\/li><li><em>[add here some angsty situation]<\/em><\/li><\/ul>\n\n\n\n<p>Yes? So, why on earth you want to&nbsp;<strong>wrap<\/strong>&nbsp;your&nbsp;<strong>already wrapped<\/strong>&nbsp;Christmas presents into another paper?!<\/p>\n\n\n\n<p>If you don&#8217;t know what are you doing, read next carefully:&nbsp;<strong>Do not wrap the wrapper.<\/strong><br>You&#8217;ll get into the troubles in deep soulful developments.<\/p>\n\n\n\n<p>If you can&#8217;t learn PDO. You can. And you have to learn. It will help your life. Trust me.<br>If you think it&#8217;s ugly. Look into the mirror and repeat&nbsp;<em>It is beautiful<\/em>&nbsp;until you&#8217;ll think so.<\/p>\n\n\n\n<p>But, but, but if you know what are you doing and you need some life easifying routines. Please create an&nbsp;<strong>extension<\/strong>&nbsp;to PDO!<\/p>\n\n\n\n<p>With an extension method you can use PDO as is, but also use functions created by you. Nothing gets broken nor your brains get damaged.<\/p>\n\n\n\n<p>I did my own simple&nbsp;<a href=\"https:\/\/github.com\/dimamedia\/PHP-PDO-extension\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">PDO extension: class.db.php<\/a>, that you can use or get inspiration for your own. I forked&nbsp;<a href=\"http:\/\/www.imavex.com\/php-pdo-wrapper-class\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Imavex&#8217;s class<\/a>&nbsp;that didn&#8217;t fit into my needs.<\/p>\n\n\n\n<p>Call class like normally&nbsp;<code>$sql = new db ();<\/code>&nbsp;with default connection parameters.<\/p>\n\n\n\n<p>Or you can call different database connection parameters like&nbsp;<code>$sqlProd = new db ('production');<\/code>&nbsp;and&nbsp;<code>$sqlDev = new db ('development');<\/code><\/p>\n\n\n\n<p>And run prepared stuff normally<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">$stmt = $sql->prepare(\"SELECT * FROM table WHERE field = ?\");\n$stmt->execute(array(\"value\"));\nprint_r($stmt->fetchAll(PDO::FETCH_ASSOC);<\/code><\/pre>\n\n\n\n<p>Or straightfully<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">$stmt = $sql->query(\"SELECT * FROM table WHERE field = 'value'\");\nprint_r($stmt->fetchAll(PDO::FETCH_ASSOC); <\/code><\/pre>\n\n\n\n<p>Or use your own functions<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">$stmt = $sql->select(\"table\", \"field = 'value'\");\nprint_r($stmt);\n\n$bind = array(':value'=>'text');\n$stmt = $sql->select(\"table\", \"field = ':value'\", $bind);\nprint_r($stmt);<\/code><\/pre>\n\n\n\n<p>Or<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">$stmt = $sql->run(\"SELECT * FROM table field = 'value'\");\nprint_r($stmt);<\/code><\/pre>\n\n\n\n<p>There is also quite a nice debugging function for all your needs. Find out more documentation on&nbsp;<a href=\"https:\/\/github.com\/dimamedia\/PHP-PDO-extension\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">github<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Do not use wrappers. Use an extension for MySQL PDO.<\/p>\n","protected":false},"author":1,"featured_media":2437,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/images.unsplash.com\/photo-1544383835-bda2bc66a55d?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjF9","fifu_image_alt":"MySQL PDO extension","footnotes":""},"categories":[133],"tags":[158,139,154,156,155,134,157],"class_list":["post-2370","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-devs","tag-class","tag-dev","tag-mysql","tag-pdo","tag-php","tag-web","tag-wrapper","has-thumbnail"],"_links":{"self":[{"href":"https:\/\/dima.fi\/blog\/wp-json\/wp\/v2\/posts\/2370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dima.fi\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dima.fi\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dima.fi\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dima.fi\/blog\/wp-json\/wp\/v2\/comments?post=2370"}],"version-history":[{"count":4,"href":"https:\/\/dima.fi\/blog\/wp-json\/wp\/v2\/posts\/2370\/revisions"}],"predecessor-version":[{"id":2387,"href":"https:\/\/dima.fi\/blog\/wp-json\/wp\/v2\/posts\/2370\/revisions\/2387"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dima.fi\/blog\/wp-json\/wp\/v2\/media\/2437"}],"wp:attachment":[{"href":"https:\/\/dima.fi\/blog\/wp-json\/wp\/v2\/media?parent=2370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dima.fi\/blog\/wp-json\/wp\/v2\/categories?post=2370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dima.fi\/blog\/wp-json\/wp\/v2\/tags?post=2370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}