从MySQL 4.1开始引入多语言的支持,但是用PHP插入的中文会出现乱码.无论用什么编码也不行.
解决这个问题其实很简单.
1.在建表的时候设置编码类型为gb2312_chinese_ci.
2.在PHP页面的数据库连接语句加一行mysql_query("SET NAMES 'gb2312'",$link); 例如
$db_host="localhost"; $db_user="root"; $db_password="password"; $db_name="test"; $link=mysql_connect($db_host,$db_user,$db_password); mysql_query("SET NAMES 'gb2312'",$link); $db=mysql_select_db($db_name,$link); $query="select * from user"; $result=mysql_query($query);
写入页面和读取页面都加入这行.这样在MYSQL里面的中文就能正常显示了.
相关资料:
从MySQL 4.1开始引入多语言的支持,而且一些特性已经超过了其他的数据库系统。
MySQL4.1的字符集支持(Character Set Support)有两个方面:字符集(Characterset)和排序方式(Collation)。对于字符集的支持细化到四个层次:服务器 (server),数据库(database),数据表(table)和连接(connection)。
查看系统的字符集和排序方式的设定可以通过下面的两条命令:!
mysql> show variables like 'character_set_%'; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | latin1 | | character_set_connection | latin1 | | character_set_database | latin1 | | character_set_results | latin1 | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 7 rows in set (0.00 sec) mysql> show variables like 'collation_%'; +----------------------+-------------------+ | Variable_name | Value | +----------------------+-------------------+ | collation_connection | latin1_swedish_ci | | collation_database | latin1_swedish_ci | | collation_server | latin1_swedish_ci | +----------------------+-------------------+ 3 rows in set (0.00 sec)
上面列出的值就是系统的默认值。(很奇怪系统怎么默认是latin1的瑞典语排序方式)...
当我们按照原来的方式通过PHP存取MySQL数据库时,就算设置了表的默认字符集为utf8并且通过UTF-8编码发送查询,你会发现存入数据库的仍然是乱码。问题就出在这个connection连接层上。解决方法是在发送查询前执行一下下面这句:
set names 'utf8';
它相当于下面的四句指令:
set character_set_client = utf8;
set character_set_results = utf8;
set character_set_connection = utf8;
set collation_connection = utf8_general_ci
由于默认网页提交的查询是gb2312(表单页面meta里可以看到),而mysql默认将其当作utf8(可以查到此时的 character_set_client=utf8),所以必然乱码。同理,mysql返回的结果是已经转换成 character_set_results编码的(与表的编码无关),同样默认是utf8,而网页页面把它当gb2312处理,所以必然有标题等由数据 库读出的字段是乱码而其他php部分文字不乱码的现象。
解决(by 一剑飘雪):
安装mysql5.0时要选utf8字符集(在用phpmyadmin创建数据库和字段时就不需要在整理中选utf8字符集了),并在php建立连接后发送
$link = mysql_connect('localhost', 'root', 'root');
mysql_query("SET NAMES 'utf8'",$link);
这时我们在网页中看到的还是乱码但已不是????了,查看网页源文件,已完全正常。用记事本打开php源文件,别存为utf8编码,再刷新网页,全部搞定了。
或者,当然还是要安装时仍要utf8安装,在php中发送set names 'gb2312',同时php文件存为记事本默认的ansi,也能正确显示中文.
但总不能每次连接时都发送一次SET NAMES 'utf8'吧,如何彻底解决还没找到方法。
这样安装mysql时缺省字符集选为utf8后又带来一个问题,我们在command.exe中进入mysql控制台后,查询结果又成了乱码,我们可以在查询前输入
mysql>set names 'gbk';
或
mysql>set names 'gb2312';
相当于告诉mysql客户端在使用gb2312字符集,结果就正确了,gb2312为GBK的子集.

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

Tracking user session activities in PHP is implemented through session management. 1) Use session_start() to start the session. 2) Store and access data through the $_SESSION array. 3) Call session_destroy() to end the session. Session tracking is used for user behavior analysis, security monitoring, and performance optimization.

Using databases to store PHP session data can improve performance and scalability. 1) Configure MySQL to store session data: Set up the session processor in php.ini or PHP code. 2) Implement custom session processor: define open, close, read, write and other functions to interact with the database. 3) Optimization and best practices: Use indexing, caching, data compression and distributed storage to improve performance.

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.


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

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

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
