A single character is giving error: "Data too long for column" when using PDO with MySQL - Why?

StackOverflow https://stackoverflow.com/questions/21316591

I'm using PHP to suck up a CSV file, massage the data, and then import it into a MySQL InnoDB table using PDO.

The table and all varchar columns have a utf8_unicode_ci collation, and are utf8.

I have a column that is varchar(1).

When I attempt using PDO to do a certain query, I'm getting SQLSTATE 22001 Data too long for column 'HeatingOption' at row 1 error.

The query is as follows:

INSERT INTO `Config`.`BaseItemCodes` 
(`BaseItemCode`, `Series`, `HeatingOption`, `StandardColor`) 
VALUES ('BD1896-1W', 'BD18', 'w', 'BG')

It's interesting to me, because I'm debugging, and having PDO print out the query to the CLI. I copy/pasted the query, without modification, directly into MySQL Workbench and executed it just fine.

I'm currently creating the value from extracting it from the end of the BaseItemCode there. I'm using substr(), trim()ing it, and strtolower()ing it as well, just to make sure of everything.

The PHP script is utf8 w/o BOM, and so is the CSV file.

I'm on Windows 8.1 using PHP 5.5.7, and running MySQL 5.6.15 on a CentOS server.

I'm thinking it's some strange charset issue, but I'm not sure what to look at next.


Edit: I just attempted to change the column to 'char(1)' and it didn't help.


Edit 2: I increased the length of the column to varchar(20) and I was able to insert the data.

I'm changing my question to "Why do MySQL and PDO behave this way?"


Edit:

I double checked the data after inserting the row and increasing the length of the column, and it seems the name of the bind :HeatingOption was being passed through. So PDO wasn't binding the data for some reason, yet to be discovered.

有帮助吗?

解决方案

the error clearly said it,

you should go to your database and change the lenth of that column HeatingOption to more then 1 , put it forexample varchar(25)

EDIT:

it should work with VARCHAR(1) or CHAR(1) however you may inserted spaces with this letter

example 'w '
           ^---//this spaces will be counted as bite, and it will be too long
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top