<?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[MySQL - 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>MySQL - 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:11:47 GMT</lastBuildDate><atom:link href="https://allurcode.com/tag/mysql/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[MySQL conditional insert]]></title><description><![CDATA[<!--kg-card-begin: markdown--><p>Sometimes I need to insert some values into the database but only when a certain condition is met. The best example is a newsletter sign up. If someone has already signed up to your newsletter, don&#x2019;t add his email address again. Of course we can run one query</p>]]></description><link>https://allurcode.com/mysql-conditional-insert/</link><guid isPermaLink="false">5b7b0aa2b52b43084c9ea13a</guid><category><![CDATA[conditional]]></category><category><![CDATA[insert]]></category><category><![CDATA[MySQL]]></category><dc:creator><![CDATA[Wojtek]]></dc:creator><pubDate>Fri, 15 Apr 2011 12:30:40 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><p>Sometimes I need to insert some values into the database but only when a certain condition is met. The best example is a newsletter sign up. If someone has already signed up to your newsletter, don&#x2019;t add his email address again. Of course we can run one query to check if this email address already exists and if not run another query to save it in our database.<br>
If for any reason you need to do the whole operation in one query things get a little more complicated.</p>
<h3 id="twoqueries">Two queries</h3>
<p>Lets say we want to insert some data only when field <code>email</code> does not equal <code>test@example.com</code><br>
First, select data:</p>
<pre><code class="language-sql">SELECT `id` FROM `table` WHERE `email` = &apos;test@example.com&apos;;
</code></pre>
<p>if this email address already exists in our database we can let our users know about it. If no records were returned we can continue to insert new data:</p>
<pre><code class="language-sql">INSERT INTO `table` (`email`) VALUES (&apos;test@example.com&apos;);
</code></pre>
<h3 id="onequery">One query</h3>
<p>In a way we combine those two above queries into one:</p>
<pre><code class="language-sql">INSERT INTO `table` (`email`) SELECT &apos;test@example.com&apos; FROM DUAL WHERE NOT EXISTS (
  SELECT `id` FROM `table` WHERE `email` = &apos;test@example.com&apos; LIMIT 1
);
</code></pre>
<p>If the subquery doesn&#x2019;t return any rows the insert will be carried out. In case of subquery finding this given email address, condition is not met and data doesn&#x2019;t get inserted.</p>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[Hot off the workshop vol. 1]]></title><description><![CDATA[<!--kg-card-begin: markdown--><p>Just launched. TK Maxx and Red Nose Day campaign applications.</p>
<h3 id="facebookquiz">Facebook quiz</h3>
<p><a href="https://apps.facebook.com/red_nose_tshirt/"><img src="https://allurcode.com/content/images/2011/02/portfolio_1_1.jpg" alt loading="lazy"></a></p>
<p>Fun quiz to find out your perfect Red Nose Day t-shirt.<br>
<a href="https://apps.facebook.com/red_nose_tshirt/">View site</a></p>
<h3 id="websiteapplication">Website application</h3>
<p><a href="https://apps.facebook.com/red_nose_tshirt/"><img src="https://allurcode.com/content/images/2011/02/portfolio_1_2.jpg" alt loading="lazy"></a></p>
<p>Upload you photo with Flash uploader, resize it and pan it around with JavaScript then save your results with PHP.<br>
<a href="https://apps.facebook.com/red_nose_tshirt/">View site</a></p>
<!--kg-card-end: markdown-->]]></description><link>https://allurcode.com/hot-off-the-workshop-vol-1/</link><guid isPermaLink="false">5b7b0aa2b52b43084c9ea132</guid><category><![CDATA[CSS]]></category><category><![CDATA[Facebook]]></category><category><![CDATA[HTML]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[MySQL]]></category><category><![CDATA[PHP]]></category><dc:creator><![CDATA[Wojtek]]></dc:creator><pubDate>Wed, 23 Feb 2011 11:51:53 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><p>Just launched. TK Maxx and Red Nose Day campaign applications.</p>
<h3 id="facebookquiz">Facebook quiz</h3>
<p><a href="https://apps.facebook.com/red_nose_tshirt/"><img src="https://allurcode.com/content/images/2011/02/portfolio_1_1.jpg" alt loading="lazy"></a></p>
<p>Fun quiz to find out your perfect Red Nose Day t-shirt.<br>
<a href="https://apps.facebook.com/red_nose_tshirt/">View site</a></p>
<h3 id="websiteapplication">Website application</h3>
<p><a href="https://apps.facebook.com/red_nose_tshirt/"><img src="https://allurcode.com/content/images/2011/02/portfolio_1_2.jpg" alt loading="lazy"></a></p>
<p>Upload you photo with Flash uploader, resize it and pan it around with JavaScript then save your results with PHP.<br>
<a href="https://apps.facebook.com/red_nose_tshirt/">View site</a></p>
<!--kg-card-end: markdown-->]]></content:encoded></item><item><title><![CDATA[MySQL very slow without a reason]]></title><description><![CDATA[<!--kg-card-begin: markdown--><p>Is your MySQL really slow without actually being too busy?<br>
Does your script take very long time to connect to the database?<br>
Do you have lots of RAM, strong CPU, just a little traffic and despite that a very long database response time?<br>
Are you seeing connections with &quot;<em>unauthenticated</em></p>]]></description><link>https://allurcode.com/mysql-very-slow-without-a-reason/</link><guid isPermaLink="false">5b7b0aa2b52b43084c9ea11e</guid><category><![CDATA[database]]></category><category><![CDATA[DNS]]></category><category><![CDATA[MySQL]]></category><category><![CDATA[slow]]></category><dc:creator><![CDATA[Wojtek]]></dc:creator><pubDate>Wed, 07 Apr 2010 16:39:49 GMT</pubDate><content:encoded><![CDATA[<!--kg-card-begin: markdown--><p>Is your MySQL really slow without actually being too busy?<br>
Does your script take very long time to connect to the database?<br>
Do you have lots of RAM, strong CPU, just a little traffic and despite that a very long database response time?<br>
Are you seeing connections with &quot;<em>unauthenticated user</em>&quot; while running <code>show processlist</code> in MySQL console?</p>
<pre><code class="language-sql">mysql&gt; show processlist;
+------+----------------------+-----------+------+---------+------+-------+------+
|  Id  |         User         |   Host    |  db  | Command | Time | State | Info |
+------+----------------------+-----------+------+---------+------+-------+------+
| 2047 | unauthenticated user | localhost | myDB | Connect |  81  |       | NULL |
| 2049 | unauthenticated user | localhost | myDB | Connect |  81  |       | NULL |
| 2050 | unauthenticated user | localhost | myDB | Connect |  76  |       | NULL |
 ... 
+------+----------------------+-----------+------+---------+------+-------+------+
131 rows in set (0.00 sec)
</code></pre>
<p>If you answered <strong>yes</strong> to any of the above questions, your MySQL might have a problem with resolving connection&#x2019;s host name.<br>
When attempt is made for a new connection, MySQL tries to resolve the host name for that request. It takes the IP address and resolves it to a host name (using <code>gethostbyaddr()</code>). It then takes that host name and resolves it back to the IP address (using <code>gethostbyname()</code>) and compares to ensure it is the original IP address.<br>
This might considerably increase connection time and slow down your whole application or produce <code>show processlist</code> results as above. You can easily solve this problem by disabling DNS host name lookups for MySQL. In order to do this you need to run your mysqld with <code>&#x2013;skip-name-resolve</code> option or add it to your <code>/etc/mysql/my.cnf</code> file like that:</p>
<pre><code>[mysqld]
skip-name-resolve
</code></pre>
<p>After that running <code>show processlist</code> will result in &quot;Host&quot; column displaying only IP addresses instead of host names and the connection speed should improve significantly.</p>
<p>Just keep in mind that also you have to change allowed hosts for your database users to proper IP addresses.</p>
<p>For additional information, check out: <a href="https://pcrevive.org/areas-we-serve/computer-repair-lake-worth-fl/">Lake Worth, Florida computer services</a></p>
<!--kg-card-end: markdown-->]]></content:encoded></item><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>