Home Backend Development PHP Tutorial php页面,mysql数据库转utf-8乱码,utf-8编码问题总结_PHP

php页面,mysql数据库转utf-8乱码,utf-8编码问题总结_PHP

May 30, 2016 am 08:44 AM

示例一:

PHP页面转UTF-8编码问题

1.在代码开始出加入一行: header("Content-Type: text/html;charset=utf-8");

2.PHP文件编码问题 点击编辑器的菜单:“文件”->“另存为”,可以看到当前文件的编码,确保文件编码为:UTF-8,如果是ANSI,需要将编码改成:UTF-8。

3.PHP文件头BOM问题: PHP文件一定不可以有BOM标签,否则,会出现session不能使用的情况,并有类似的提示:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent 这是因为,在执行session_start() 的时候,整个页面不能有输出,但是当由于前PHP页面存在BOM标签,PHP把这个BOM标签当成是输出了,所以就出错了! 所以PHP页面一定要删除BOM标签

删除这个BOM标签的方法:

1.可以用Dreamweaver打开文件,并重新保存,即可以去除BOM标签!

2.可以用EditPlus打开文件,并在菜单“首选项”->“文件”->"UTF-8标识",设置为:“总是删除签名”,然后保存文件,即可以去除BOM标签!

3.PHP以附件形式保存文件的时候,UTF-8编码问题: PHP以附件形式保存文件,文件名必须是GB2312编码,否则,如果文件名中有中文的话,将是显示乱码: 如果你的PHP本身是UTF-8编码格式的文件,需要将文件名变量由UTF-8转成GB2312: iconv("UTF-8", "GB2312", "$filename");

4.截断显示文章标题时,出现乱码或者“?”问号的问题:

一般文章标题很长的时候,会显示一部分标题,会对文章标题进行截断,由于一个UTF-8编码格式的中文字符会占用3个字符宽度,截取标题的时候,有时会只截取到一个中文字符的1个字符或2字符宽度,没截取完整,将出现乱码或“?”问号的情况,

用下面的函数截取标题,就不会有问题:

function get_brief_str($str, $max_length) { 
  echo strlen($str) . ""; 
  if (strlen($str) > $max_length) { 
    $check_num = 0; 
    for ($i = 0; $i < $max_length; $i++) { 
      if (ord($str[$i]) > 128) 
        $check_num++; 
    } 
    if ($check_num % 3 == 0) 
      $str = substr($str, 0, $max_length) . "..."; 
    else 
      if ($check_num % 3 == 1) 
        $str = substr($str, 0, $max_length +2) . "..."; 
      else 
        if ($check_num % 3 == 2) 
          $str = substr($str, 0, $max_length +1) . "..."; 
  } 
  return $str; 
}
Copy after login

MYSQL数据库使用UTF-8编码的问题

1.用phpmyadmin创建数据库和数据表 创建数据库的时候,请将“整理”设置为:“utf8_general_ci”或执行语句:

CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
Copy after login

创建数据表的时候:如果是该字段是存放中文的话,则需要将“整理”设置为:“utf8_general_ci”,如果该字段是存放英文或数字的话,默认就可以了。

相应的SQL语句,例如:

CREATE TABLE `test` ( 
`id` INT NOT NULL , 
`name` VARCHAR( 10 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , 
PRIMARY KEY ( `id` ) 
) ENGINE = MYISAM ;
Copy after login

2.用PHP读写数据库

在连接数据库之后:

$connection = mysql_connect($host_name, $host_user, $host_pass);
Copy after login

加入两行:

mysql_query("set character set 'utf8'");//读库 
mysql_query("set names 'utf8'");//写库
Copy after login

就可以正常的读写MYSQL数据库了。

示例二:

php+mysql的utf-8中文乱码问题的解决方法

问题汇总:

1.mysql数据库默认的编码是utf8,如果这种编码与你的PHP网页不一致,可能就会造成MYSQL乱码.

2.MYSQL中创建表时会让你选择一种编码,如果这种编码与你的网页编码不一致,也可能造成MYSQL乱码.

3.MYSQL创建表时添加字段是可以选择编码的,如果这种编码与你的网页编码不一致,也可能造成MYSQL乱码.

4.用户提交页面的编码与显示数据的页面编码不一致,就肯定会造成PHP页面乱码.

5.如用户输入资料的页面是big5码, 显示用户输入的页面却是gb2312,这种100%会造成PHP页面乱码.

6.PHP页面字符集不正确.

7.PHP连接MYSQL数据库语句指定的编码不正确.

使用mysql+php产生乱码的原因都了解得很清楚了,那么解决就不困难了.

针对不同问题的解决方法:

1.mysql数据库默认的编码是utf8,如果这种编码与你的PHP网页不一致,可能就会造成MYSQL乱码.

修改数据库编码,如果是数据库编码不正确,可以在phpmyadmin 执行如下命令:

Alter DATABASE 'test' DEFAULT CHARACTER SET utf8 COLLATE utf8_bin
Copy after login

以上命令就是将test数据库的编码设为utf8.

2.MYSQL中创建表时会让你选择一种编码,如果这种编码与你的网页编码不一致,也可能造成MYSQL乱码.

修改表的编码:

Alter TABLE 'category' DEFAULT CHARACTER SET utf8 COLLATE utf8_bin
Copy after login

以上命令就是将一个表category的编码改为utf8.

3.MYSQL创建表时添加字段是可以选择编码的,如果这种编码与你的网页编码不一致,也可能造成MYSQL乱码.

修改字段的编码:

Alter TABLE 'test' CHANGE 'dd' 'dd' VARCHAR( 45 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL
Copy after login

以上命令就是将test表中 dd的字段编码改为utf8.

4.用户提交页面的编码与显示数据的页面编码不一致,就肯定会造成PHP页面乱码.

如果是这种情况容易解决,只需检查下页面,修改源文件的charset即可.

5.如用户输入资料的页面是big5码, 显示用户输入的页面却是gb2312,这种100%会造成PHP页面乱码.

这种情况也是修改页面charset即可.

6.PHP页面字符集不正确.

为了避免PHP页面乱码的发生,PHP页面开始第一句

header("content-type:text/html; charset=utf-8");
Copy after login

//强行指定页面的编码,以避免乱码

7.PHP连接MYSQL数据库语句指定的编码不正确.

在连接数据库的语句中.

mysql_connect('localhost','user','password');
mysql_select_db('my_db');
mysql_query("set names 'utf8'"); //select 数据库之后加多这一句
Copy after login

以上内容就是本文给大家介绍php页面,mysql数据库转utf-8乱码,utf-8编码问题总结,希望大家喜欢。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1248
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

See all articles