search
HomeBackend DevelopmentPHP Tutorial PHP查询mysql,中文数据,通过不同方式添加的相同数据竟然显示不同解决方案

PHP查询mysql,中文数据,通过不同方式添加的相同数据竟然显示不同
hi,
我先说下我的情况。php的新手。
----现象
输出的页面中,我已经添加了,我也在查询mysql的时候,添加了
mysql_query("set character set utf8");
 mysql_query("SET NAMES 'UTF8'");
有的中文可以显示,有的不能。

----乱码显示的过程和代码
我的phpmyadmin中数据库和text字段,都是用 utf8_unicode_ci进行设置。

我手动通过phpmyadmin在数据库添加的中文,然后直接执行
 mysql_query("set character set utf8");
 mysql_query("SET NAMES 'UTF8'");
$rs = $db->query("SELECT * FROM book");
显示出乱码。

----正常显示的代码和意外的结果
 mysql_query("set character set utf8");
 mysql_query("SET NAMES 'UTF8'");
$rs2 =$db->exec("INSERT INTO `lalabook`.`book` (`bookname`, `bookdes`) VALUES ('小兵', '兵小')");
$rs = $db->query("SELECT * FROM book");
可以正常的显示出我插入的数据。但是有个问题是,我在通过phpmyadmin查看我插入的数据时,显示的时乱码。
----
所以我不清楚这个时什么原因造成的,是不是我以后插入数据都要通过代码。不能再用phpmyadmin插入了


------解决方案--------------------
在phpmyadmin 内直接执行 SET NAMES 'UTF8' 然后再SELECT ....出来的结果呢?
phpmyadmin也是需要设置链接编码的.
------解决方案--------------------
是不是文件本身的编码有问题
------解决方案--------------------
是虚拟主机吧, 一切都是正常的.

用户提交Utf8,php操作入库,出库就可以了。

phpadmin乱码是因为mysql_server编码一般服务商都是latin的,但服务商一般都保证connenct/client/result是utf8的,也就是数据出入的编码是utf8,存储的时候是latin,所以去phpadmin看php操作的会乱码。

另外,在phpadmin里执行sql插入的数据,在php读取的时候会乱码,这个问题是很神奇的,我也没看phpmyadmin页面是什么编码。

一句话:设置库,表,列的编码全部是utf8-unicode-ci,之后数据的出入库都采用utf8编码,不要在意phpadmin查数据时候乱码,不会影响用户即可。
------解决方案--------------------
编码常用的是utf8-general-ci
不只是表要通一,要统一的总共有四处
一是:数据库类型,其中包括,数据库,表,字段三处都要统一,可以检查一下
二是:文件的编码类型,你若用dw或editplus可以查看页面编码,不同需修改
三是:访问数据库时的设置既set NAMES utf8;
四是:浏览器显示方式,既
这四处必需统一,否则可能会出现问题

Statement
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
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

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

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

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),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor