为什么PHP 处理数据库 读出"张三"的编码而不是 汉字?
$con = mysql_connect("localhost","root","1234");
mysql_select_db("test",$con);
mysql_query("set names utf8");
mysql_query("set character_set_client=utf8");
mysql_query("set character_set_results=utf8");
$q=mysql_query("SELECT * FROM people");
while($e=mysql_fetch_assoc($q)){
$output[]=$e; }
print(json_encode($output));
mysql_close();
?>
输出结果:
[{"id":"10","name":"John","sex":"1","birthyear":"1979"},{"id":"11","name :"\u5f20\u4e09","sex":"1","birthyear":"1989"}]
为什么不是 张三
回复讨论(解决方案)
因?你json_encode了,中文??未union??
print(json_encode($output));
改成
print($output);
如果你json_encode后也想看到中文,可以?考我之前?的文章: http://blog.csdn.net/fdipzone/article/details/28766357
json_encode 只接受 utf-8 编码的数据
json_encode 在做 json 编码时,会将多字节的 utf-8 字符转换成双字节的 unicode 编码的实体形式
这样在任何环境中bouquet不会因编码的原因造成数据的失真
json 数据格式主要用于与 js 通讯,而浏览器会将任何编码的数据转换成其工作字符集 unicode
所以,json_encode 的转码工作有利于减轻浏览器的压力
$con = mysql_connect("localhost","root","1234");
mysql_select_db("test",$con);
mysql_query("set names utf8");
mysql_query("set character_set_client=utf8");
mysql_query("set character_set_results=utf8");
$q=mysql_query("SELECT * FROM people");
while($e=mysql_fetch_assoc($q)){
$output[]=$e; }
print(json_encode($output));
mysql_close();
?>
输出结果:
[{"id":"10","name":"John","sex":"1","birthyear":"1979"},{"id":"11","name :"\u5f20\u4e09","sex":"1","birthyear":"1989"}]
为什么不是 张三
为什么我重装了wamp后, 又变成了 乱码, 反而倒退了? 请问上面的代码哪里有问题么?还是有哪个文件要配置?
在哪里看到有乱码?
在哪里看到有乱码?
莫名其妙的没有乱码了。。 现在是解码出问题了
解码出了什么问题?
解码出了什么问题?
谢谢版主,刚解决了解码的问题,为了使得PHP在浏览器输出时候,能显示中文,我在最前面加入了 echo ""; 原来这句话也是被android 接收了。是不是通过Php打印出的东西都被发送给了android客户端?
是的,php 打印的任何内容都被发往客户端

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Analysis of the basic principles of the MySQL database management system MySQL is a commonly used relational database management system that uses structured query language (SQL) for data storage and management. This article will introduce the basic principles of the MySQL database management system, including database creation, data table design, data addition, deletion, modification, and other operations, and provide specific code examples. 1. Database Creation In MySQL, you first need to create a database instance to store data. The following code can create a file named "my

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

Hellofolks, my name is Luga, and today we will talk about technologies related to the artificial intelligence (AI) ecological field - GenAI. Facing the challenges of rapid technological innovation and differentiated business scenarios, traditional coding methods have begun to become acclimated and cannot fully cope with the growing demands. At the same time, emerging general-purpose GenAI (artificial intelligence technology) has great potential to meet this demand. As a representative of artificial intelligence technology, GenAI has begun to be widely used in all walks of life with its strong potential and capabilities. It can automatically learn and adapt to coding needs in different scenarios, greatly improving coding efficiency and quality. Through deep learning and model optimization, GenAI is able to accurately understand different

PHP is a back-end programming language widely used in website development. It has powerful database operation functions and is often used to interact with databases such as MySQL. However, due to the complexity of Chinese character encoding, problems often arise when dealing with Chinese garbled characters in the database. This article will introduce the skills and practices of PHP in handling Chinese garbled characters in databases, including common causes of garbled characters, solutions and specific code examples. Common reasons for garbled characters are incorrect database character set settings: the correct character set needs to be selected when creating the database, such as utf8 or u
