Saturday, April 26, 2008

Importing UTF-8 Datasets in MySQL

Before importing a UTF-8 dataset, be sure to change the default MySQL database collation to utf8_unicode_ci.

Note: If the tables and structures are in the utf8 collation but the database is on a Latin collation, the import will not be successful.

The database, tables and structures need to be all UTF8 enabled.

Saturday, April 19, 2008

Escape MySQL Variables in the Same Sequence

When escaping a MySQL query, be sure to escape the variables in the correct order.

Example:

UPDATE
table_name
SET
var1='%s',
var3='%s',
var2='%s'
WHERE
foo=bar
mysql_real_escape_string($var1, $db),
mysql_real_escape_string($var3, $db),
mysql_real_escape_string($var2, $db)

The mysql_real_escape_string function will escape variables in the order specified in SET