The error message when creating a database connection using PHP's mysqli is displayed as garbled characters.
The code is as follows:
<code><?php $conn = new mysqli('localhost', 'xxxxx', 'xxxxxx'); ?></code>
mysql has not been installed yet, just want to see the error message of mysqli when mysql is not installed. The result is as follows:
is garbled in both chrome and IE11.
The charset in the http response header obtained from the inspection tool is utf-8, and the default settings of chrome and IE11 are also utf-8. Additionally, the php file itself and the default encoding settings for Apache and PHP are all utf-8.
Currently it is speculated that the problem is with mysqli itself, because the garbled code in the picture is consistent with the garbled code obtained by mysqli_connect_error()
.
The results obtained by mb_detect_encoding($conn->connect_error)
checking the encoding show that the encoding of the error message is also utf-8.
How to solve it?
No offense intended, but since many respondents rush to conclusions without carefully reading the question description, I decided to repeat something important:
I just want to know what error message mysqli gets without MySQL installed. However, this error message is actually garbled, so this problem arises.
Progress: When running with the php interpreter in the console, the correct error message can be obtained, and it is found to be in Chinese, as shown in the figure below:
This error message is provided by the network communication related module of the Windows system (in the past, such error messages were often seen in the log when the proxy server configuration was incorrect), so it is visually estimated that Win10 is responsible for this.
The problem is still not solved. Although it is Chinese, I tried to display it in GB2312, GBK, and GB18030, but it was also garbled, but the garbled content was different.
The error message when creating a database connection using PHP's mysqli is displayed as garbled characters.
The code is as follows:
<code><?php $conn = new mysqli('localhost', 'xxxxx', 'xxxxxx'); ?></code>
mysql has not been installed yet, just want to see the error message of mysqli when mysql is not installed. The result is as follows:
is garbled in both chrome and IE11.
The charset in the http response header obtained from the inspection tool is utf-8, and the default settings of chrome and IE11 are also utf-8. Additionally, the php file itself and the default encoding settings for Apache and PHP are all utf-8.
Currently it is speculated that the problem is with mysqli itself, because the garbled code in the picture is consistent with the garbled code obtained by mysqli_connect_error()
.
The results obtained by mb_detect_encoding($conn->connect_error)
checking the encoding show that the encoding of the error message is also utf-8.
How to solve it?
No offense intended, but since many respondents rush to conclusions without carefully reading the question description, I decided to repeat something important:
I just want to know what error message mysqli gets without MySQL installed. However, this error message is actually garbled, so this problem arises.
Progress: When running with the php interpreter in the console, the correct error message can be obtained, and it is found to be in Chinese, as shown in the figure below:
This error message is provided by the network communication related module of the Windows system (in the past, such error messages were often seen in the log when the proxy server configuration was incorrect), so it is visually estimated that Win10 is responsible for this.
The problem is still not solved. Although it is Chinese, I tried to display it in GB2312, GBK, and GB18030, but it was also garbled, but the garbled content was different.
In your environment, is it normal to use PHP to output Chinese?
Have you added header('Content-type: text/html; charset=utf-8');
?
Try changing Chrome’s encoding manually?
I tried it on my computer and came to three conclusions: 1. Your PHP page did not use HTML’s charset to set the page to UTF8 encoding, because what PHP ultimately feeds back to the browser is still pure HTML. 2. What is returned is not utf8 at all. For example, in the returned gbk, you set utf8 on the page, but the browser is GB2312. 3. There may be a problem with your configuration and you didn't find it, and we I can't see the information related to this on your computer. Just finish the question. There is no need to blindly let everyone try it.
It should be enough to set the browser encoding. It should not be the reason for mysqli, and the writing method is also wrong
The default encoding of the MySQL database you installed is GBK. It is recommended to change the default encoding to ensure the unification of front-end, back-end and database encoding. If the unified setting still does not work, then MySQL needs to query a set names utf 8
before execution. Ensure that the buffer content is in utf8 format
I didn’t see that MySQL was not installed in the author’s question, so I deleted it.
But it should actually be because of the mysqlnd extension
Since the character set is returned during handshake, it should be related to the system character set. It is recommended to install MySQL first and try it. After installation, specify the string utf8
Add encoding settings to the database connection settingsmysqli_query('set name UTF8');