Home > Article > Backend Development > How to solve database php garbled code
How to solve the garbled database php?
Recently I am using Apache php MySql to practice website writing. During the process of storing data, the Chinese entered in the HTML form is stored in the database through php and is displayed as garbled characters. I searched on Baidu and found various opinions. Yes, I was really worried, but I still saw some useful information, that is, whether it is Chinese, English or other characters, the encoding format is set to utf8.
Recommended: "PHP Tutorial"
1. Add this sentence to the html code:
Figure 1 Figure 2 2 . If you forget to set this attribute when creating the table, don't worry. There is another way. First, select the table whose attributes you want to modify, click Operations to modify its attributes, and at the same time, you can check changeall column and confirm, as shown in Figure 2. In order to further ensure that no garbled characters appear, you can also add utf8-general-ci to some fields or all fields in the table. Specific operation: Click the table that needs to be modified (tb_product)->Structure->check all->change, and then set the Collation of each field individually. As shown in Figure 3. Figure 3 Note: If you do not select encoding as utf8-general-ci for the entire table, but only for the table Some fields are set to utf8-general-ci format. If you check the table attributes through Operation, you will still see that the encoding of the entire table is: For example, if the format is gbk-chinese-ci, if the Html encoding is utf8, it will still be Garbled characters appear, so the most important thing is to set the Collation of the entire table to utf8-general-ci. As shown in Figure 4 and Figure 5, although pro_name and pro_producer appear to be utf8-general-ci encoded in Figure 4, by looking at the encoding of the entire table, it is still in gbk format. The encoding is inconsistent, so it is possible that garbled characters will still appear in the table. Okay, let’s summarize, to make a long story short, four points: 1. If you write php or html code in zend studio, first change the display encoding format of the created project to utf8 format; 2. Use in html to display the encoding format of the web page to be written; 3. When connecting to the database, after selecting a database (not a table), use mysql_qurry("set names utf8"); 4. When creating the database mentioned above, change the entire table The encoding format is set to utf8-general-ci. Picture 4 Picture 5
The above is the detailed content of How to solve database php garbled code. For more information, please follow other related articles on the PHP Chinese website!