Home > Backend Development > PHP Tutorial > PHP中运行Mysql语句中文出错问题

PHP中运行Mysql语句中文出错问题

WBOY
Release: 2016-06-06 20:41:57
Original
1091 people have browsed it

在PHP中运行一段Mysql语句,如下:

<code>$sql = "SELECT * FROM  `users` WHERE user = 'liang'";
$return = mysqli_num_rows($sql);
</code>
Copy after login
Copy after login

当user字段内容为英文时,可完美运行。当user字段为中文时,则无法正常运行。

求教各位大大,在此多谢了!

回复内容:

在PHP中运行一段Mysql语句,如下:

<code>$sql = "SELECT * FROM  `users` WHERE user = 'liang'";
$return = mysqli_num_rows($sql);
</code>
Copy after login
Copy after login

当user字段内容为英文时,可完美运行。当user字段为中文时,则无法正常运行。

求教各位大大,在此多谢了!

  1. 先将文件改为utf-8编码;
  2. 在连接语句后立即执行下面的语句:

    <code>mysqli_query('set names utf8');
    </code>
    Copy after login

首先确定是编码的问题, 当编码情况有很多种,

character set clientDocumentation utf8mb4
character set connectionDocumentation utf8mb4
character set databaseDocumentation utf8mb4
character set filesystemDocumentation binary
character set resultsDocumentation utf8mb4
character set serverDocumentation utf8mb4
character set systemDocumentation

编码不能一概的用 set names XXX 来解决,
要清楚 PHP 本身的编码, 发送给数据库的编码, 数据库存储等编码等,

找到编码不同的部分, 转换编码解决,.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template