示例一:
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; }
MYSQL数据库使用UTF-8编码的问题
1.用phpmyadmin创建数据库和数据表 创建数据库的时候,请将“整理”设置为:“utf8_general_ci”或执行语句:
CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
创建数据表的时候:如果是该字段是存放中文的话,则需要将“整理”设置为:“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 ;
2.用PHP读写数据库
在连接数据库之后:
$connection = mysql_connect($host_name, $host_user, $host_pass);
加入两行:
mysql_query("set character set 'utf8'");//读库 mysql_query("set names 'utf8'");//写库
就可以正常的读写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
以上命令就是将test数据库的编码设为utf8.
2.MYSQL中创建表时会让你选择一种编码,如果这种编码与你的网页编码不一致,也可能造成MYSQL乱码.
修改表的编码:
Alter TABLE 'category' DEFAULT CHARACTER SET utf8 COLLATE utf8_bin
以上命令就是将一个表category的编码改为utf8.
3.MYSQL创建表时添加字段是可以选择编码的,如果这种编码与你的网页编码不一致,也可能造成MYSQL乱码.
修改字段的编码:
Alter TABLE 'test' CHANGE 'dd' 'dd' VARCHAR( 45 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL
以上命令就是将test表中 dd的字段编码改为utf8.
4.用户提交页面的编码与显示数据的页面编码不一致,就肯定会造成PHP页面乱码.
如果是这种情况容易解决,只需检查下页面,修改源文件的charset即可.
5.如用户输入资料的页面是big5码, 显示用户输入的页面却是gb2312,这种100%会造成PHP页面乱码.
这种情况也是修改页面charset即可.
6.PHP页面字符集不正确.
为了避免PHP页面乱码的发生,PHP页面开始第一句
header("content-type:text/html; charset=utf-8");
//强行指定页面的编码,以避免乱码
7.PHP连接MYSQL数据库语句指定的编码不正确.
在连接数据库的语句中.
mysql_connect('localhost','user','password'); mysql_select_db('my_db'); mysql_query("set names 'utf8'"); //select 数据库之后加多这一句
以上内容就是本文给大家介绍php页面,mysql数据库转utf-8乱码,utf-8编码问题总结,希望大家喜欢。

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

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 reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools