VAR or VARCHAR, which to choose?
While I was optimizing the database for the application I am currently developing, I thoroughly check the differences between these two types, VAR and VARCHAR, so might as well share my notes here. Keep in mind that these are for MySQL 5.0.3 and later, things were different in certain cases before.
CHAR
- string, length from 0 to 255
- required storage depends on the set length
- values are right-padded with spaces to the specified length when stored
- trailing spaces are removed when values are retrieved
- values that exceeds the specified length are truncated, warning is generated (strict SQL mode disabled, otherwise error and not stored)
VARCHAR
- string, length from 0 to 65,535 (but subject to maximum row size which is shared among all columns)
- required storage depends on the stored value
- values are not padded when stored
- trailing spaces are retained when values are stored or retrieved
- values that exceeds the specified length are truncated, warning is generated (strict SQL mode disabled, otherwise error and not stored)
Finally, a couple examples to illustrate it all:
mysql > CREATE DATABASE sandbox;
mysql > USE sandbox;
mysql > CREATE TABLE vc (`c` char(3), `v` char(3));
mysql > INSERT INTO vc VALUES ('', ''), (' ', ' '), ('a', 'a'), ('a ', 'a '), ('abc', 'abc');
mysql > SELECT CONCAT('(', c, ')'), CONCAT('(', v, ')');
That last query will return:
+---------------------+---------------------+
| CONCAT('(', c, ')') | CONCAT('(', v, ')') |
+---------------------+---------------------+
| () | () |
| () | ( ) |
| (a) | (a) |
| (a) | (a ) |
| (abc) | (abc) |
+---------------------+---------------------+
Notice how for CHAR the trailing space is removed. Another interesting thing to note, sorting and comparing CHAR and VARCHAR columns:
mysql > SELECT c = 'a ', v = 'a ' FROM vc;
+---------------------+
| c = 'a ' | v = 'a ' |
+---------------------+
| 0 | 0 |
| 0 | 0 |
| 1 | 1 |
| 1 | 1 |
| 0 | 0 |
+---------------------+
As you can see, it compares values without regard to any trailing spaces.
There, that should resume it all and hopefully help next time there is a field type to set. Did I forget to mention anything? Let me know in the comments.
Beware of the underscore
Have you ever tried using underscores (_) in your controllers variables that you are passing to the view using a combination of Controller::set() and compact()? Well, if you did, you must have realized that they never make it there, right? I haven’t submitted it as a bug yet but here it is:
class SandboxController extends AppController {
var $uses = null;
var $layout = 'sandbox';
function beforeRender(){}
function index()
{
$foo = 'foo';
$bar = 'bar';
$foo_bar = 'foobar';
$this->set(compact('foo', 'bar', 'foo_bar'));
$john = 'john';
$doe = 'doe';
$john_doe = 'john_doe';
$this->set(compact('john', 'doe'));
$this->set('john_doe', $john_doe);
}
}
Now, in your view:
echo $foo . "<br />";
echo $bar ."<br />";
echo $foo_bar ."<br />";
echo "<br /><br />";
echo $john ."<br />";
echo $doe ."<br />";
echo $john_doe ."<br />";
Finally, the returned output:

Anyone knows if that’s how it’s supposed to act?
Update: there is a way to actually get those variables intact. Thanks to Taylor for pointing it out.
$this->set(compact($foo, $bar, $foo_bar), false);
My current development environment - suggestions?
While I was away, I wasn’t 100% on break… Among the work related stuff I’ve done was revamping my whole dev environment, hardware as well as software.
For a little over a decade now, I have been a Windows user. In the early days, it was because Mac was just too expensive for what my parents could afford for my toys; but since all the Mac-frenzy started, it was more out of fear of loosing some precious time adapting to a new OS (and all what comes with it) than anything else really. I finally decided to give it a try early this year with a Mac mini to start with but that didn’t impress me much at first, mainly because I was never really giving it a real chance to. Only a month or two later, when I got the MacBook Air as a gift from my partner, that I really started enjoying and appreciating the new OS.
I went ahead and purchased the Mac OS X, installed that on the mini, hoping that it will push me into using that little machine some more until I identified the real reason behind my constant bouncing back to Windows: multiple displays. I know you have ’spaces’ in Mac but I still prefer the physical black separation that I get from working with 2 screens (back then), the same reason why I’d rather have 2 x 21″ screens instead of one big display. To my big deception, the mini was not fit for the multiple displays setting because of it’s low-end graphic card - same for the Air.
Around the same time, I was revamping my desk, making it more ergonomic and much simpler. A solid wood board with a metallic bar going horizontally across to support the extra weight, 2 metallic frames for the legs, a keyboard/mouse tray (#02-ORB439BK) w/ ball bearing arm (#02-ARMLKS) and a multi-display desk mount (#30-500-B16-04). The multiple displays were used back then as follow: one on the mini, 2 for the PC and one switching between the FreeBSD box and the docked laptop. Given the fact that I could run all these OS from a central machine, I decided it was time to invest in a real beast, the Mac Pro, for which I had to add an extra VGA card, a wireless ethernet card (- )can you believe this machine comes with no wireless included?) and some RAM.

Once all that was plugged, the only PC left around was a Toshiba laptop. The temptation to bounce back to Windows was slowly dying and the current setup was only contributing to that.
Now, a couple months later and after having toyed with different tools, here are the ones that I felt worked well for me:
- Adium
- Calendar
- Firefox 3 (add-ons: Read it later, Firebug)
- [Fireworks CS4]
- MySQL Administrator
- Office 2008
- Parallels (running: DBDesigner 4 on WinXP)
- Skype
- TextMate (bundles: CakePHP, FTP/SSH, Trac)
- Time Capsule
And finally, I keep all files (local sandbox, remote projects, etc.) in SVN repositories for easy access to my files from anywhere. That pretty much covers it all. Since I am new to Mac, maybe there are stuff I’m missing out on, if so, please share. Also, what’s your approach on having your development environment follow you (files, tools, etc.)?
Summer’s over - and so is my break!
Hi everyone,
I know my posts have been few and far between over the last several months, that’s because I never really felt like getting back to work since I started my break in mid-December ‘07. Who would like to get back to work, right? I have to admit, it felt GOOD! But summer is over now, fall officially started yesterday and for me that means only one thing: it’s time to start getting ready for hibernation mode!
Why hibernation mode? Because, for one, I don’t do any winter sport (and I don’t see myself starting one at this age with a semi-handicapped elbow), I rarely have the need to get out to do things (there’s always someone for every task) and, to be honest, who likes to stick his nose out by -30 celsius? Exactly, not many, when given the option.
And basically, now that I’m seriously back, you shall see a lot more coming from me on this blog in the days, weeks and months to come. Maybe finally doing the move to it’s own CakePHP-based CMS and domain?
Hack into any MacOS X
Early this week, I finally went and purchased Leopard to upgrade my Mac Mini and start running the same MacOS version on all computers I use. Everything was going good until the automated system reboot. Somehow, on the login prompt, my root access wasn’t being authorized anymore.
A simple search quickly revealed I wasn’t the only one that ran into this kind of issue, every Mac user that had a password of 8 characters and more faced the same problem. But now what’s the solution when your root password is a long one?
You will be relieved to hear that Apple’s software engineers already thought of that. Actually, I don’t know how much thought was put into it, but there is a solution available. The only problem is that this same solution can be used by anyone on a computer running MacOS X (haven’t tried older versions) to gain root access.
I’ll let you judge for yourselves.
Back from a long break…
Wow - my last post was on December 6th, over 5 months ago! Having to write after all this time makes me feel like it was the first post ever… Where to start? Nowhere.
I can see that things have been moving pretty quickly in the CakePHP world: CakePHP 1.2 Beta, 1st CakeFest (damn, I missed that!) and last but not least, the Cookbook.
My highlights of the past 5 months:
- Moved back to Montreal
- Migrated from WinXP to Vista (very short) to Mac OS X
- Massive workstation upgrade
- Took a loooong break from work
- Hired new staff
- Moving soon to its own domain
- Mike will start blogging in the coming weeks (my latest recruit on the development team)
- Getting a facelift sooner than later
HtmlSource - a new DBO driver for CakePHP
Ok, ok - I’ve been slacking on this blog again, but I will keep that for another post where I will announce some major changes I have been thinking of lately. For today, I’d like to introduce the new DBO Source Driver: HtmlSource - which is completely functional but still lacking some of the features I have planned for it.
So what’s an HTML DBO driver you ask?
Simply put, it’s a way to treat any HTML page like a database and be able to retrieve (scrape) certain parts using an SQL-like command:
SELECT href, title FROM a WHERE class="submit"
PHP releases 5.2.5 to fix multiple vulnerabilities
For the ones of you who have not opted to receive the PHP announcements from the php.net site, here’s an important one you shouldn’t miss if you are using the 5.2.x branch.
From the PHP team:
over 60 bug fixes, several of which are security relatedSome of the vulnerabilities are:
- Various errors exist in the “htmlentities” and “htmlspecialchars” functions where partial multibyte sequences are not accepted.
- Various boundary errors exist in the “fnmatch()”, “setlocale()”, and “glob()” functions and can be exploited to cause buffer overflows.
- An error in the processing of “.htaccess” files can be exploited to bypass the “disablefunctions” directive by modifying the “mail.forceextraparameters” php.ini directive via an “.htaccess” file.
- An error in the handling of variables can be exploited to overwrite values set in httpd.conf via the “iniset()” function.
GridHelper - for easy grids in CakePHP
Been busy as hell and I’m afraid it will be like this for another month or so, but here is a quick one I felt like sharing because I hope it will be helpful to some.
Haven’t you ever wished to only have to write something like this in your views:
e($grid->create($results));Continue reading…
Conventional solution for the visitors + AclComponent
At different places where I read about ACL, a common question that always comes early in the comments is:
Assuming ‘Guests’ users are unidentified web visitors, how do you handler their access rights? They don’t login, so they can’t be assigned an ARO and thus ACL will reject their access to any actions.
How do you handle ‘Guest’ user’s permissons if they are not logged in?Continue reading…

