MySQL/PDO truncates the data

前端 未结 2 2004
心在旅途
心在旅途 2021-01-18 16:39

$book is a 7kb string. If this query is executed using PHP PDO exec, the monograph column (LONGTEXT) data gets truncated at 6765 chara

相关标签:
2条回答
  • 2021-01-18 17:16

    There are two points to be made here. One is that ideally all character encodings must be UTF8 - that's server, client, connection, and table. Two is that PHP's strlen function counts bytes, not characters.

    Your table character set may not be set to UTF8. You can do

    SHOW CREATE TABLE chemicals;
    

    to check that. You should also add these to your my.cnf:

    [mysqld]
    character-set-client=utf8
    character-set-results=utf8
    

    Read more about MySQL character sets here:

    MySQL character sets

    0 讨论(0)
  • 2021-01-18 17:29

    It turned out to be that it is encoding issue. The are two solutions. The most obvious is to fix the encoding to match the database/connection settings. In my case, I was getting a iso-8859-1 string and interpreting it as a unicode.

    However, it shouldn't be an issue anyway. I went further to discover that PDO::ATTR_EMULATE_PREPARES is set to TRUE by default.

    • You will find this topic as a useful summary to understand what does the latter setting mean, PDO MySQL: Use PDO::ATTR_EMULATE_PREPARES or not?.
    • This topic will explain why the encoding messed up the query, The way PDO parametrized query works.
    0 讨论(0)
提交回复
热议问题