Question

I have a column VARCHAR(1000) in MySQL DB. So, when PHP requests insert more than 1000 symbols, the rest of the text is rejected.

On the site I had placed a textarea with maxlength=1000, but on the server side, PHP parse submitted text with the htmlspecialchars function, so if the text was it's called "nothing", it becomes it's called "nothing".

The problem is text could become more than 1000 symbols even it was typed clearly 1000 characters.

Can you help me find the right way, right function etc. to insert all characters which user typed?

Was it helpful?

Solution

Don't use htmlspecialchars when storing in the database. Use it (or htmlentities) when retrieving from the DB and writing to the browser.

OTHER TIPS

You must also watch out for UTF-8 which can have characters that take up more space than one byte each.

The idea behind VARCHAR (over CHAR for example) is that it allows the data base to store less bytes to disk when the field is not fully used.

For example, if you write "xx" into VARCHAR(1000), only two characters are stored to your physical data base, not 1000. (Note, I said characters here not bytes. The actual number of bytes will be more to allow for wider variable width UTF-8 codes, the needed string length, any word and disk buffer padding, etc. But clearly you will be storing a lot less bytes than if you used CHAR.)

So let SQL do some of the work for you. Make the field size 3000 or something, or at least big enough that you will never have a problem, even if a large percentage of the symbols are wider than 8 bits.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top