JavaScript-form character length vs. database field length

Source: Internet
Author: User
HTML form length is calculated by the number of characters, whether it is Chinese characters or letters, but the database is also in bytes, the Chinese characters accounted for 2 letters accounted for 1, so it is easy to write the length of the problem.

Two questions:
1. Is there a good way to calculate length using a uniform method between the front end or PHP or MySQL?
2. Is it only possible to set the database length sufficiently large that the form will never be exceeded?

.

Reply content:

HTML form length is calculated by the number of characters, whether it is Chinese characters or letters, but the database is also in bytes, the Chinese characters accounted for 2 letters accounted for 1, so it is easy to write the length of the problem.

Two questions:
1. Is there a good way to calculate length using a uniform method between the front end or PHP or MySQL?
2. Is it only possible to set the database length sufficiently large that the form will never be exceeded?

.

After MySQL is 4.1, char and varchar define the character length.
Specifically, you can look at the document the CHAR and VARCHAR Types.
And 4.1 of the documents are directly mentioned.

The CHAR and VARCHAR types is declared with a length this indicates the maximum number of characters you want to store. For example, CHAR (+) can hold up to characters. (Before MySQL 4.1, the length is interpreted as number of bytes.)

The uniform calculation of length is based on the concept of "length" is the same standard, first you have to specify what your "length" refers to.
If "Length" refers to the length of the byte, then this can be done (assuming the page encoding is UTF8):

// javascriptfunction lengthInUtf8Bytes(str) {  // Matches only the 10.. bytes that are non-initial characters in a multi-byte sequence.  var m = encodeURIComponent(str).match(/%[89ABab]/g);  return str.length + (m ? m.length : 0);}len = lengthInUtf8Bytes(str);//php$len = strlen($str);//mysqlCREATE DATABASE `byte_test`    CHARACTER SET latin1     COLLATE latin1_general_ci;

JavaScript function lengthinutf8bytes from string length in bytes in JavaScript
If "Length" refers to the length of the encoding, or if the page UTF8 encoded:

// javascriptlen = str.length;// php$len = mb_strlen($str,"UTF-8");// mysqlCREATE DATABASE `utf8_test`    CHARACTER SET utf8    COLLATE utf8_general_ci;

This length is consistent, so it's important to define what your "length" means at the very beginning.

Front desk limit is just a fake, database fight get longer, not too stingy

If you use UTF-8 encoding for unification, there is no such distress. database table with UTF-8, a Chinese character or letter is one, that is, varchar (2) type, you can certainly put the word "test" deposit. The actual storage for MySQL will use 6 bytes, usually a Chinese character needs to be stored in 3 bytes, but this is what the MySQL bottom does, it is not related to the length of the varchar within your definition.

2 floor good professional, praise

Database fields do not have to be so accurate, say 10 bytes must limit 10 bytes, limit the length is through the program, database fields just provide a scope

If you can also need to check the parameters in the background, others can directly request the background service interface.
The maxlength for the front end can control the input length.

If you are not afraid of trouble,
You can design a generic interceptor yourself to check for pre-submission,
First, you get the metadata information for the database, such as the encoding and the field length (this information that can be cached is generally not changed).
Then according to this figure out the corresponding field can hold a few characters, a few English characters.
After that, the corresponding number of bytes is calculated in the front-end or PHP, so there is no problem.
However, there is a slight loss in performance.
A better approach would be to use variable-length types such as the varchar design as large enough for indefinite lengths of data.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.