Archive for 2009

MySQL: Two more ways to find out table storage engine

In addition to SHOW TABLE STATUS there are at least two more ways to find out a table storage engine.

The first way is simply to query the INFORMATION_SCHEMA.TABLES.

SELECT `table_name`, `engine`
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = DATABASE();

In case there is not permission to access INFORMATION_SCHEMA the second way requires only the SELECT privilege for the table.

SHOW CREATE TABLE TestTable;

MySQL: An alternative to alter

A while ago I was involved in the technical part of an interview with a PHP developer. Obviously some questions were about MySQL. For instance – how to add a new column to existing table. The candidate tried to convince us that there was no way to do that except creating another table with new column and copy data from old table there. Sure thing that we advised him to read about ALTER command.

However it turned out that suggested ‘method’ could have sense. According to Chris at Everything MySQL blog a full dump and reload to a table with new column may be much more faster than alter table! For instance there is shown a test with 5Gb table and alter table is 47% slower. Of course such ‘alternative alter’ should be used only against slave as well as some other restrictions may apply.

Below is the suggested process.

1. If you have a slave, AND YOU SHOULD AT THIS POINT, run stop slave
2. Run the SELECT INTO OUTFILE local, if you have the space, or over nfs (not very fast)
3. DROP or RENAME THE TABLE you are trying to ALTER
— RENAME only if you have the space and you don’t trust THE PROCESS!
4. CREATE the same table with the ALTERATIONS, if you dropped, or a new table with the ALTERATIONS
5. IMPORT the file from step 2
6. Either RENAME the two tables, if you renamed, or start slave
7. Fail over the writes to the newly altered slave
8. Repeat steps 1 – 7 for the old master

The Vendor Client relationship – in real world situations

How an Engineer folds a T-Shirt

RSS cheese

If you get bored with your blog RSS icon or just want a new or unusual one, check out a free icon set RSS Cheese.

According to the author the set contains 4 high resolution RSS icons that come in four sizes: 512×512, 256×256, 128×128 and 64×64 as well as icons are completely free and can be used without any restrictions in any type of project inlcuding commercial projects.

RSS Cheese

PHP: Don’t repeat parameters using sprintf()

While missing a C# method String.Format() I’ve found a similar function in PHP, i.e. sprintf(). But since this function is slightly different from String.Format() it is not obvious how to avoid duplicating the same parameters.

For instance how to re-write an example below without passing $url and $title twice.

$link = sprintf('<a href="%s" title="%s">%s</a> (%s)</li>',
              $url, $title, $title, $url);

The answer is less obvious compare to C# but still pretty handy:

$link = sprintf('<a href="%1$s" title="%2$s">%2$s</a> (%1$s)',
              $url, $title);

In both cases echo $link give the same output like:

<a href="http://www.google.com/" title="Google">Google</a> (http://www.google.com/)

but the second way makes the code more elegant 🙂 .

PHP: json_encode before 5.2.0

Even though it is a lot of talks about PHP 5.3 there are still many production environments which run PHP 5.1.*. As well as sometimes development servers are updated earlier than production ones for testing purposes. It may cause that some new PHP features will not work on production because of older version there.

One of the most common examples is ‘missing’ json_encode() function which became native with PHP 5.2.0 onward. Below is possible workaround while your production has not upgraded yet.

<?php
if (!function_exists('json_encode'))
{
  function json_encode($a=false)
  {
    if (is_null($a)) return 'null';
    if ($a === false) return 'false';
    if ($a === true) return 'true';
    if (is_scalar($a))
    {
      if (is_float($a))
      {
        // Always use "." for floats.
        return floatval(str_replace(",", ".", strval($a)));
      }
 
      if (is_string($a))
      {
        static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
        return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
      }
      else
        return $a;
    }
    $isList = true;
    for ($i = 0, reset($a); $i < count($a); $i++, next($a))
    {
      if (key($a) !== $i)
      {
        $isList = false;
        break;
      }
    }
    $result = array();
    if ($isList)
    {
      foreach ($a as $v) $result[] = json_encode($v);
      return '[' . join(',', $result) . ']';
    }
    else
    {
      foreach ($a as $k => $v) $result[] = json_encode($k).':'.json_encode($v);
      return '{' . join(',', $result) . '}';
    }
  }
}
?>

Update: the same code is now available through GitHub.

IKEA cars

IKEA car

The Ugly Truth

Th Ugly Truth movie turned out to be furnished with good soundtracks.

Don’t forget to check a complete list of soundtracks.

Sweet Dream Sudoku

Sweet Dream Sudoku

« Previous PageNext Page »