Solutions to Chinese garbled characters in PHP
2. Add mysql_query("set names 'encoding'"); before the PHP program that needs to perform database operations. The encoding is consistent with the PHP encoding. If the PHP encoding is gb2312, then the mysql encoding is gb2312. If it is utf-8, then the mysql encoding is utf8, so that there will be no garbled characters when inserting or retrieving data 3. PHP is related to the operating system The encoding of Windows and Linux is different. In the Windows environment, errors will occur if the parameters are UTF-8 encoded when calling PHP functions, such as move_uploaded_file(), filesize(), readfile(), etc. These functions are processing uploads. , is often used when downloading, and the following error may occur when calling: Warning: move_uploaded_file()[function.move-uploaded-file]: failed to open stream: Invalid argument in … Warning: move_uploaded_file()[function.move-uploaded-file]:Unable to move ” to ” in … Warning: filesize() [function.filesize]: stat failed for … in … Warning: readfile() [function.readfile]: failed to open stream: Invalid argument in .. Although these errors will not occur when using gb2312 encoding in a Linux environment, the saved file name will be garbled and the file cannot be read. In this case, the parameters can be converted into the encoding recognized by the operating system. The encoding conversion can be done with mb_convert_encoding(string, New encoding, original encoding) or iconv (original encoding, new encoding, string), so that the file name saved after processing will not be garbled, and the file can be read normally to achieve uploading and downloading of Chinese name files. In fact, there is a better solution, which is to completely separate from the system, and there is no need to consider the encoding of the system. You can generate a sequence of only letters and numbers as the file name, and save the original name with Chinese characters in the database. In this way, there will be no problem when calling move_uploaded_file(). When downloading, you only need to change the file name to the original name with Chinese characters. Chinese name. The code to implement downloading is as follows
$file_type is the type of file, $file_name is the original name, and $file_path is the address of the file saved on the service. 4. Why are the characters garbled? Generally speaking, there are two reasons for the appearance of garbled characters. First, it is due to an error in the encoding (charset) setting, which causes the browser to parse with the wrong encoding, resulting in a messy "heavenly book" that fills the screen. Second, the file is Open and then save in the wrong encoding. For example, a text file was originally encoded in GB2312, but it was opened in UTF-8 encoding and then saved. To solve the above garbled code problem, you first need to know which aspects of development involve coding: 1. File encoding: refers to the encoding in which the page file (.html, .php, etc.) itself is saved. Notepad and Dreamweaver will automatically recognize the file encoding when opening the page, so there will be less problems. However, ZendStudio does not automatically recognize the encoding. It will only open the file in a certain encoding according to the configuration of the preferences. If you accidentally open the file with the wrong encoding while working, and save it after making the modification, garbled characters will appear ( I feel it deeply). 2. Page declaration encoding: In the HTML code HEAD, you can use to tell the browser what encoding the web page uses. Currently, XXX mainly uses GB2312 and UTF-8 in Chinese website development. 3. Database connection encoding: refers to the encoding used to transmit data to the database when performing database operations. It should be noted here that it should not be confused with the encoding of the database itself. For example, the default encoding within MySQL is latin1, which means that Mysql uses latin1. Encoding to store data, data transmitted to Mysql in other encodings will be converted to latin1 encoding. Knowing where coding is involved in web development, you also know the reasons for garbled codes: the above three coding settings are inconsistent. Since most of the various codings are ASCII compatible, English symbols will not appear, and Chinese characters will be out of luck. . 5. Common error situations and solutions 1. The database uses UTF8 encoding, and the page declaration encoding is GB2312. This is the most common cause of garbled characters. At this time, the direct SELECT data in the PHP script will be garbled. You need to use: mysql_query("SET NAMES GBK"); before querying to set the MYSQL connection encoding and ensure that the page declaration encoding is consistent with the connection encoding set here ( GBK is an extension of GB2312). If the page is UTF-8 encoded, you can use: mysql_query("SET NAMES UTF8"); Note that it is UTF8 rather than the commonly used UTF-8. If the encoding of the page declaration is consistent with the internal encoding of the database, you do not need to set the connection encoding. Note: In fact, the data input and output of MYSQL is more complicated than what is mentioned above. There are 2 default encodings defined in the MYSQL configuration file my.ini, which are default-character-set in [client] and default in [mysqld]. -character-set to set the encoding used by default for client connections and internal databases respectively. The encoding we specified above is actually the command line parameter character_set_client when the MYSQL client connects to the server, which tells the MYSQL server what encoding the client data received is, instead of using the default encoding. 2. The page declaration encoding is inconsistent with the encoding of the file itself. This rarely happens because if the encoding is inconsistent, what the artist sees in the browser when creating the page will be garbled characters. More often than not, it is caused by fixing some minor bugs after release, opening the page in the wrong encoding and then saving it. Or you use some FTP software to directly modify files online, such as CuteFTP. Due to incorrect software encoding configuration, the wrong encoding is converted. 3. Some friends who rent virtual hosts still have garbled codes even though the above three encodings are set correctly. For example, if the web page is encoded in GB2312, it is always recognized as UTF-8 when opened by browsers such as IE. The HEAD of the web page has already stated that it is GB2312. After manually changing the browser encoding to GB2312, the page displays normally. The reason is that the server Apache sets the global default encoding of the server and adds AddDefaultCharset UTF-8 in httpd.conf. At this time, the server will first send the HTTP header to the browser, and its priority is higher than the encoding declared in the page. Naturally, the browser will recognize it incorrectly. There are two solutions. Administrators should add AddDefaultCharset GB2312 to the configuration file of their own virtual machine to override the global configuration, or configure it in .htaccess in their own directory. Summary: The best and fastest way to solve PHP Chinese garbled code is to require the encoding declared on the page to be consistent with the internal encoding of the database. If the page number requested by the page is inconsistent with the internal encoding of the database, set the connection encoding, mysql_query("SET NAMES XXX "); XXX is the connection encoding, which can definitely solve the problem of garbled characters. |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.
