Archive for July, 2009
Creating XLS Files With PHP
For many years, whenever one of my web applications needed to output tabular data I’d rely on CSV format. This does the trick for most data, as Excel and most other spreadsheet programs will open CSV’s without a problem. What happens when you want to get a little fancier though? Maybe include some formatting on columns, or even embed images?
In that case CSV doesn’t cut it anymore. I had to investigate some methods of generating a valid XLS file directly with PHP.
Validate Dollar Amount with PHP
It can be hard when conducting interviews for a web developer position to really know a candidates skill level. Recently a company presented me with the following problem, which I thought stood head and shoulders above the usual multiple choice quizzes and simple logic problems I’ve been presented with.
In the language of your choice, write a function that accepts a string. The string is a dollar amount, which may start with a dollar sign, and may have commas in it. Output should be a number, or false if there are illegal characters in the string.
My first instinct was to look for a PHP built in that would do the above, but surprisingly enough, there was none! So on to my solutions.
ERROR 1146 (42S02): Table ‘mysql.servers’ doesn’t exist
I’m not sure what caused this error in the first place, as it cropped on a server I don’t admin..
ERROR 1146 (42S02): Table ‘mysql.servers’ doesn’t exist
This cropped up on Server version: 5.1.36 MySQL Community Server (GPL) by Remi, installed on CentOS whenever I would execute the ‘FLUSH PRIVILEGES’ command.
The solution? Try this.
USE mysql; CREATE TABLE `servers` ( `Server_name` char(64) NOT NULL, `Host` char(64) NOT NULL, `Db` char(64) NOT NULL, `Username` char(64) NOT NULL, `Password` char(64) NOT NULL, `Port` int(4) DEFAULT NULL, `Socket` char(64) DEFAULT NULL, `Wrapper` char(64) NOT NULL, `Owner` char(64) NOT NULL, PRIMARY KEY (`Server_name`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='MySQL Foreign Servers table';
Apparently this issue arises sometimes with an incomplete MySQL upgrade. Beyond just installing new binaries you’ll often need to run scripts/mysql_fix_privilege_tables to ensure the ‘mysql’ database contains all the needed stuff.
