<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[PDO - All your code are belong to us]]></title><description><![CDATA[Thoughts, stories and ideas on code and technology in general.<br>Blog title inspired by <a href="https://en.wikipedia.org/wiki/All_your_base_are_belong_to_us" target="_blank">this meme</a>]]></description><link>https://allurcode.com/</link><image><url>https://allurcode.com/favicon.png</url><title>PDO - All your code are belong to us</title><link>https://allurcode.com/</link></image><generator>Ghost 4.48</generator><lastBuildDate>Tue, 09 Jun 2026 08:43:28 GMT</lastBuildDate><atom:link href="https://allurcode.com/tag/pdo/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[PDO rowCount() not working in PHP 5.1.6]]></title><description><![CDATA[<!--kg-card-begin: markdown--><h3 id="thebug">The bug</h3>
<p>If you&#x2019;re running PHP 5.1.6 and just started using PDO for your database connection, it&#x2019;s likely you&#x2019;ll run into quite an annoying bug.<br>
Lets test a simple query partly taken from PHP documentation.</p>
<pre><code class="language-php">$calories = 150;
$colour = &apos;red&apos;;
$sth</code></pre>]]></description><link>https://allurcode.com/pdo-rowcount-not-working-in-php-5-1-6/</link><guid isPermaLink="false">5b7b0aa2b52b43084c9ea11d</guid><category><![CDATA[bug]]></category><category><![CDATA[MySQL]]></category><category><![CDATA[PDO]]></category><category><![CDATA[PHP]]></category><category><![CDATA[rowcount]]></category><dc:creator><![CDATA[Wojtek]]></dc:creator><pubDate>Tue, 30 Mar 2010 23:41:50 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><h3 id="thebug">The bug</h3>
<p>If you&#x2019;re running PHP 5.1.6 and just started using PDO for your database connection, it&#x2019;s likely you&#x2019;ll run into quite an annoying bug.<br>
Lets test a simple query partly taken from PHP documentation.</p>
<pre><code class="language-php">$calories = 150;
$colour = &apos;red&apos;;
$sth = $myPDO-&gt;prepare(&apos;SELECT name, colour, calories FROM fruit WHERE calories &lt; :calories AND colour = :colour&apos;);
$sth-&gt;bindParam(&apos;:calories&apos;, $calories, PDO::PARAM_INT);
$sth-&gt;bindParam(&apos;:colour&apos;, $colour, PDO::PARAM_STR, 12);
$sth-&gt;execute();
if ($sth-&gt;rowCount() &gt;= 1) {
  // iterate through results
}
</code></pre>
<p>Everything seems great. The problem is that <code>$sth-&gt;rowCount()</code> will <strong>always</strong> return 0. No matter how many results your query returns, the value of <code>rowCount()</code> will always be 0.</p>
<p>Full bug report can be found here: <a href="https://bugs.php.net/40822">https://bugs.php.net/40822</a>.</p>
<h3 id="solution">Solution</h3>
<p>Upgrade PHP &#x1F642; If that&#x2019;s not an option read on.<br>
Lets create our own simple PDO and PDOStatement classes.</p>
<pre><code class="language-php">class myPDO extends PDO {
  function __construct($name_host, $username=&apos;&apos;, $password=&apos;&apos;, $driverOptions=array()) {
    parent::__construct($name_host, $username, $password, $driverOptions);
    $this-&gt;setAttribute(PDO::ATTR_STATEMENT_CLASS, array(&apos;myPDOStatement&apos;, array($this)));
  }
}

class myPDOStatement extends PDOStatement {
  public $db;
  protected function __construct($db) {
    $this-&gt;db = $db;
  }
}
</code></pre>
<p>Above will not fix anything. This is just a start so we can overload some PDO methods to apply the fix.</p>
<p>To fix the bug we need to tell MySQL to use the buffered versions of the MySQL API by setting attribute <em>MYSQL_ATTR_USE_BUFFERED_QUERY</em> to <em>true</em>.<br>
For some reason, still unknown to me, setting this option like that</p>
<pre><code class="language-php">class myPDO extends PDO {
  function __construct($name_host, $username=&apos;&apos;, $password=&apos;&apos;, $driverOptions=array()) {
    parent::__construct($name_host, $username, $password, $driverOptions);
    $this-&gt;setAttribute(PDO::ATTR_STATEMENT_CLASS, array(&apos;myPDOStatement&apos;, array($this)));
    $this-&gt;setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
  }
}
</code></pre>
<p>will <strong>not</strong> work.</p>
<p>After many hours of tearing my hair out I found out that all you need to do is this:</p>
<pre><code class="language-php">class myPDO extends PDO {
  function __construct($name_host, $username=&apos;&apos;, $password=&apos;&apos;, $driverOptions=array()) {
    $driverOptions[PDO::MYSQL_ATTR_USE_BUFFERED_QUERY] = true;
    parent::__construct($name_host, $username, $password, $driverOptions);
    $this-&gt;setAttribute(PDO::ATTR_STATEMENT_CLASS, array(&apos;myPDOStatement&apos;, array($this)));
  }
}
</code></pre>
<p>You have to set <em>MYSQL_ATTR_USE_BUFFERED_QUERY</em> before instantiating a connection. Setting it after the connection was made will not work.</p>
<p>When this is done we need to modify our <code>myPDOStatement</code> class.</p>
<pre><code class="language-php">class myPDOStatement extends PDOStatement {
  public $db;
  // will hold number of affected rows
  private $foundRows;
  
  protected function __construct($db) {
    $this-&gt;db = $db;
  }
  
  public function execute($array = null) {
    if ($array === null) {
      $result = parent :: execute();
    } else {
      $result = parent :: execute($array);
    }
    // fix for PHP 5.1.6 rowCount error
    $this-&gt;foundRows = $this-&gt;db-&gt;query(&quot;SELECT FOUND_ROWS()&quot;)-&gt;fetchColumn();
    return $result;
  }
  
  public function rowCount() {
    return $this-&gt;foundRows;
  }
}
</code></pre>
<p>After above changes our <em>rowCount()</em> method will return proper values.</p>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[MySQL PHP PDO error in xampp]]></title><description><![CDATA[<!--kg-card-begin: markdown--><p>If your PDO scripts are crashing Apache after installing xampp 1.7 for windows all you need to do is:</p>
<ul>
<li>download this package <a href="http://windows.php.net/downloads/releases/php-5.2.11-nts-Win32-VC6-x86.zip">http://windows.php.net/downloads/releases/php-5.2.11-nts-Win32-VC6-x86.zip&quot;</a></li>
<li>unzip</li>
<li>copy <code>libmysql.dll</code> into <code>xampp\apache\bin</code> and <code>xampp\php</code></li>
<li>restart apache</li>
</ul>
<p>All should be</p>]]></description><link>https://allurcode.com/mysql-php-pdo-error-in-xampp/</link><guid isPermaLink="false">5b7b0aa2b52b43084c9ea114</guid><category><![CDATA[MySQL]]></category><category><![CDATA[PDO]]></category><category><![CDATA[PHP]]></category><category><![CDATA[xampp]]></category><category><![CDATA[Windows]]></category><dc:creator><![CDATA[Wojtek]]></dc:creator><pubDate>Thu, 25 Jun 2009 11:59:09 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><p>If your PDO scripts are crashing Apache after installing xampp 1.7 for windows all you need to do is:</p>
<ul>
<li>download this package <a href="http://windows.php.net/downloads/releases/php-5.2.11-nts-Win32-VC6-x86.zip">http://windows.php.net/downloads/releases/php-5.2.11-nts-Win32-VC6-x86.zip&quot;</a></li>
<li>unzip</li>
<li>copy <code>libmysql.dll</code> into <code>xampp\apache\bin</code> and <code>xampp\php</code></li>
<li>restart apache</li>
</ul>
<p>All should be good now.</p>
<!--kg-card-end: markdown-->]]></content:encoded></item></channel></rss>