search

Home  >  Q&A  >  body text

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

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

$sql = "SELECT * FROM  `users` WHERE user = 'liang'";
$return = mysqli_num_rows($sql);

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

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

巴扎黑巴扎黑2925 days ago394

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-10 15:06:29

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

      mysqli_query('set names utf8');
      

    reply
    0
  • 黄舟

    黄舟2017-04-10 15:06:29

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

    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 本身的编码, 发送给数据库的编码, 数据库存储等编码等,

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

    reply
    0
  • Cancelreply