Skip to content


Creating a MySQL dump in CSV format

Mostly, a dump of a db is wanted in SQL. In case of emergency import file. But some people do not comprehend SQL or their SQL doesn’t like your SQL and everything goes down the drain. So there is the CSV or Comma-separated values file. As the name says, it separates the values by commas (and more if needed). Since it’s so dead simple, you will get a lot of different tools and program languages that will make life easy for you to re-import or just search in it. Microsoft Excel and Openoffice Calc both can handle the format as well, so for a quick look, this will do very nicely.

But there is no simple –csv switch in mysqldump, your weapon of choice for these tasks. So here the command that will allow you to do what you are after:

mysqldump -p -u USER -T DIRECTORY --fields-enclosed-by=\" --fields-terminated-by=, DATABASE

So this is the short version, and here what it all means:

  • -p : Asks for a password, as most users have one. If you don’t specify this on a database with a user/password, it will error
  • -u USER : Replace USER with your actual username to connect to the database.
  • -T DIRECTORY : This creates a tab delimited file. Not what we wanted but it’s the base we need.
  • –fields-enclosed-by=\” : Will add ” characters around the fields. This will allow CSV implementations to find everything that fits together. You will need that backslash or it wont run.
  • –fields-terminated-by=, : The so much sought after comma. This replaces the tab and puts a comma in its place, which, you guessed it, creates the CSV file.
  • DATABASE : Well you know, the thing this is all about…

To actually be able to do it though, you will need the FILE privilege on this database. Armed with this, you should be able to do your CSV exports easy now.

Posted in MySQL. Tagged with , .

Electronic Arts Confirms Battlefield 3

Finally. Electronic Arts confirms that Battlefield 3 is in development. And it only took a long while to get to it. But then again, I started my little Battlefield 3 page a long time ago.

Let’s hope it’s as deep as Battlefield 2 and just as flexible as Battlefield 2142. Maybe also as fun as those destruction sprees you can do in Battlefield Bad Company as well.

Posted in Gaming. Tagged with .

Cycling between strings

I’m certain every programmer who wanted to something a bit more fancy came across this problem. You want to cycle through 2 or more values, and then start from the beginning again, but all of this easy and fast. The standard implementation I have seen so far is to define a variable, then count it up until it has reached a also predefined maximum value, and reset it to the start value. Rinse repeat.

Often this is paired with an array and keys to find the correct content. Sometimes a function is used as a wrapper (using static variables more often than not) and similar rather ugly things.

Well, what does keep a state in itself very well? A class of course.

On Aidan Lister’s page I cam across a very interesting piece of code that cycles between strings. You instantiate the class with the values to cycle through given to the constructor. Now from here on out all you do is use the variable, holding the object directly like it would contain the value you need directly, say an integer or a string. Through the magic method __toString() it then dispenses the current value and advances the pointer, or wraps it around to the start.

Very simple yet powerful and also some food for thought, as this exact method might work well on other problems.

Posted in PHP.

Mini caching in classes

I just found myself a bit in a bind on what to do. I have a list, generated from the database that is returned by a method. Now this method is called multiple times from two different places (out of scope) so I either have to globalize the result somehow, like make it an attribute in a higher up class or pass it around. Both are very unappealing and rather ugly to maintain.

Instead, I opted for a mini cache in the originating class. The setup is quiet simple, so I now also use this in base classes that get implemented down the line.
Continued…

Posted in PHP. Tagged with .

2009 has 53 weeks

I had to write a tool that will add weeks to a database, where records that get added in the future will have to be linked with those week records. So a cron job runs and creates about 150 weeks in advance, running once a month, leaving the previously inserted weeks alone.

Upon testing though there was a weird little snag.
Continued…

Posted in PHP.

Large MySQL Inserts with PHP

You have to insert 1 Million entries into a database. You know there is a structure to it (I’ll supply one as a sample) so the easiest is to write a PHP script to do this task for you. Said and done, the script is finished and it does the job. But slow as hell. Not only does it trash your hard disk like mad, it also times out on a browser since you don’t want to do the shell rumba. You have only 20k entries in your database, wasted 30 seconds (while the database still tries to catch up with the sent queries) and now you realize you need a few more rows anyways.

To make life easy, I’ll use Zend Framework for this. You could use anything else that sends queries, but since I already used it for this project, there was little sense in doing something else.  So, with all useless stuff stripped out, here the code in it’s first, rather infancy way.

Continued…

Posted in MySQL, PHP, Zend Framework.

Everyone got something to say

So everyone got themselves a blog. I tried already once before, but this is just not my thing. I just don’t want to post daily or even weekly.

Still, there are things that are interesting to me and sometimes I have spent considerable time and nerves in something which might come in handy down the road for someone else. Or for myself, again. So besides the occasional rant, my content here is simple and straight forward.

Solutions to problems we didn’t know we had.

Posted in General.