Posted: February 27th, 2010 | Author: Jop | Filed under: MySQL | No Comments »
Just a quick note on character encoding in MySQL, use the following to force MySQL into using UTF-8 for ever!
PHP
In your PHP, always use the following immediately after you set up your connection.
- mysql_set_charset(‘utf8′, $oDB);
- mysql_query(‘SET NAMES \’utf8\”);
The tricky bit lies in your MySQL configuration file:
my.cnf
Add the following lines:
[client]
default-character-set=utf8
[mysqld]
default-character-set=utf8
default-collation = utf8_general_ci
This tells MySQL that the client (which is either PHP or the system) expects UTF-8. If you don’t tell MySQL this, a mysqldump will produce ANSI and an import from the command line will result in double encodings.
And don’t forget to use utf8_general_ci for all databases, tables and fields!
Posted: July 17th, 2009 | Author: rick | Filed under: MySQL | 7 Comments »
Recently I needed to create an SQL statement which was to count the occurences of a character inside a particular database field. For example, you could store the tags connected to a single db-record in a semi-colon (;) separated column in the record. To get the number of tags for this record straight from the database, we would thus like the number of semi-colons in the field (+1) as an indicator for this count. Since MySQL5 does not have a standard function for this (similar to PHP’s substr_count()), I had to find something of my own. Read the rest of this entry »
Posted: June 26th, 2009 | Author: jeroen | Filed under: MySQL | Tags: MySQL | No Comments »
Also tired of always looking up the minimum and/or maximum value of a MySQL integer field? For once and for all a cheatsheet:
| Field type |
Bytes |
Attribute |
Min. value |
Max. value |
| TINYINT |
1 |
|
-128 |
(-2^7) |
127 (2^7 – 1) |
| TINYINT |
1 |
UNSIGNED |
0 |
|
255 (2^8 – 1) |
| SMALLINT |
2 |
|
-32768 |
(-2^15) |
32767 (2^15 – 1) |
| SMALLINT |
2 |
UNSIGNED |
0 |
|
65535 (2^16 – 1) |
| MEDIUMINT |
3 |
|
-8388608 |
(-2^23) |
8388607 (2^23 – 1) |
| MEDIUMINT |
3 |
UNSIGNED |
0 |
|
16777215 (2^24 – 1) |
| INT |
4 |
|
-2147483648 |
(-2^31) |
2147483647 (2^31 – 1) |
| INT |
4 |
UNSIGNED |
0 |
|
4294967295 (2^32 – 1) |
| BIGINT |
8 |
|
-9223372036854775808 |
(-2^63) |
9223372036854775807 (2^63 – 1) |
| BIGINT |
8 |
UNSIGNED |
0 |
|
18446744073709551615 (2^64 – 1) |