Monthly Archives: June 2010

Pear DB and “DB Error: already exists” On Insert

Note that this post relates to using a MySQL database in conjunction with the PEAR DB module, or any other PEAR module that depends on DB – like DB_TABLE (which I’ve been enjoying).

You might run into this error if you switch over from using PHP’s built-in MySQL functions, over to a database abstraction layer, which is what I did on a recent project. After much banging-the-head-on-the-desk, I finally figured out that PEAR DB doesn’t use the same auto-increment sequence that the actual MySQL engine does.

For example, you may have a database using a CMS that doesn’t use PEAR. You do your various inserts, and inevitably, your deletes. So, your auto-increment column might start out in a perfect sequence like 1 – 2 – 3 – 4 – 5, etc. But then say the first two get deleted, so now your sequence id’s are 3 – 4 – 5. But then you insert again, and your CMS, using the DBMS directly, knows to use 6 for the next auto-increment value.

But then say you switch over to PEAR DB, and why wouldn’t you? It’s great – and readily bundled into most PHP installations. But then you notice, taking the above example, that your next insert starts at 1. Then the next insert produces an id of 2. And then you get to 3 and WHOA what happens, “DB Error: already exists” that’s wtf happens. PEAR DB ignores MySQL’s auto-increment sequence and makes its own (jump down to the notes/warnings in red).

You may also notice that you have table names with “_seq” at the end.

Why is this? I have no idea – but it’s stooooopid. Is there some reason for this? Seriously, please enlighten me! I mean, what’s an example of where you’d like to use more than 1 sequence?

seqSo what to do? You need to alter those “_seq” tables, that’s what. So if you’re having trouble inserting into a table named “products” – get MySQL’s “Next Autoindex” value, which you can easily do in phpMyAdmin on a table’s “structure” tab (see the screenshot, where it’s 20).

Now, go into the products_seq table, and change the id to that number (20 in this case). Now you shouldn’t have any problems.

So the moral of the story is, if you start out using a PEAR database abstraction layer, you’ll never have this problem. But if you’re unlucky enough to not have done that in the first place and switch over to it, you may have this issue and this is a quick fix.

Fancybox: Reload Parent Page When Closed

This caused me a bit of a headache. I knew you had to use the onClosed option in your Fancybox call, but wasn’t sure how to use it. For example, if you have a page full of images, and use Fancybox to add/edit those images, then you want that parent page to refresh when you close the Fancybox to show any changes you made.

In this example, I attach a Fancybox to any link with the class “editImage.” When it’s closed, the parent will refresh:

$("a.editImage").fancybox({
  'width': 800,
  'height': 450,
  'onClosed': function() {
    parent.location.reload(true);
  }
});

If you’re not sure what this means or how to use Fancybox, you should hop over here.

Update August 10, 2011:

Note that if you have something like this in your file:

$(".iframe").fancybox({
  'width': 800,
  'height': 400   
});

That it should go before anything else, otherwise if your fancy box link has both of these classes, the iframe class will cancel out anything you declare before it. So in this example, if my link was like <a class=”iframe editImage” href=”whatever”> then in my javascript, I’d want to declare the iframe before the editImage, like so:

$(".iframe").fancybox({
  'width': 800,
  'height': 400   
});

$("a.editImage").fancybox({
  'width': 800,
  'height': 450,
  'onClosed': function() {
    parent.location.reload(true);
  }
});

TextExpander Stopped Working? Try this.

Suddenly I noticed that my beloved TextExpander just stops working/expanding for no apparent reason. Restarting the app didn’t work, but what does work is restarting Firefox.

I guess there’s some conflict between Firefox version 3.6.x and the TextExpander engine. So the quick fix seems to be to just restart Firefox when this happens.

Evidently, this isn’t fixed in TE3 – so no need to upgrade with hope of this getting fixed (at least not of this writing).

Cheers!

UPDATE January 4, 2011:
It has been pointed out in the comments that closing Chrome can also fix the problem – thanks folks!