Tekker Blog

My little home on the internet.

Archive for the ‘PHP’ Category

Another MySQL wtf, staring php5!

without comments

Again, I was working on a client project (the same project as this post) on my Windows workstation running AppServ. I ran across some unicode that I needed to insert into MySQL. Did it work? You guessed it, it sure didn’t! Turns out things like © and ® are not things php+mysql care for.  I found a few suggestions while googling to “SET NAMES utf8;” and “SET CHARACTER SET utf8;” after connecting to the database.  It didn’t work.

Example:

CREATE TABLE `blah` (
`id` int(11) NOT NULL auto_increment,
`blah` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

php code:

mysql_connect('localhost', 'blah', 'blah') || die (mysql_error());
mysql_query('SET NAMES utf8;');
mysql_query('SET CHARACTER SET utf8');
mysql_select_db('blah') || die (mysql_error());
mysql_query('insert into blah (blah) values (\'this © and this ® suck\')') || die (mysql_error());

The result of the insert was was ‘this ‘.  It cuts the string off at ©.

I ended up just replacing the extra tags with super coding skills like str_replace(‘©', '').  I am aware of the different stuff that php has to break that down to © and ®. However I was dealing with urls that had these characters in it and running those functions against the whole string would do fun things like turn & into & and what happens if you already have an & in the url because a value is already url encoded?

Fun times.  Fun times indeed.

Written by Paul

September 25th, 2008 at 9:22 am

Posted in MySQL,PHP

Tagged with , ,