varchar vs text - MySQL

前端 未结 4 1037
后悔当初
后悔当初 2021-01-14 12:04

in my project an user can write comment [plain text], and view others comment, can delete own comment, but can not update comment !
In this case which would should i use

4条回答
  •  礼貌的吻别
    2021-01-14 12:21

    To protect yourself from XSS attack, encode it using the htmlentities function.

    Other than that, the choice of datatype has most to do with how big the content will be. If it may exceed 4048 characters, then use a text datatype. If many posts will be large, using a text datatype may reduce wasted data space and may perform slightly better than a giant varchar, but it depends upon your situation, you would be best to test the alternatives.

    I generally prefer varchar because it's easier to deal with from a coding perspective, if nothing else, and fall back to text if the contents may exceed the size of a varchar.

提交回复
热议问题