php - 如何将特殊字符以UTF-8格式存储在数据库中

标签 php mysql iis utf-8 rich-text-editor

我正在尝试将“£”存储在数据库中。我还在集合中将此字段标记为utf8_general_ci。但它保存为�。我还在元标记 HTML 文件中设置了 UTF-8。这里缺少什么

-----------------编辑------------------------

我检查过,该页面上有一个编辑器(富文本)。如果我使用这个编辑器保存数据,那么它会生成 � 但如果我使用文本框,那么它工作正常。我不知道这出了什么问题以及如何调试这个问题

$query = "insert into field_master set fk_t_id = '".$_POST['t_id']."', fk_g_id = '".$_POST['g_id']."', fk_f_id = '".$_POST['f_id_'.$inc_i.'_'.$inc_j]."'".$str.", field_value = '".$field_value."', field_required = '".$required."', fk_section_id = '".$_POST['section_id_'.$inc_i]."', section_order = '".$_POST['section_order_'.$inc_i]."', field_order = '".$_POST['temp_order_'.$inc_i.'_'.$inc_j]."'";
$smt = $class->dbh->prepare($query);
$smt->execute();

最佳答案

The examples shown here assume use of the utf8 character set and utf8_general_ci collation.

Specify character settings per database. To create a database such that its tables will use a given default character set and collation for data storage, use a CREATE DATABASE statement like this:

CREATE DATABASE mydb
  DEFAULT CHARACTER SET utf8
  DEFAULT COLLATE utf8_general_ci;
Tables created in the database will use utf8 and utf8_general_ci by default for any character columns.

Applications that use the database should also configure their connection to the server each time they connect. This can be done by executing a SET NAMES 'utf8' statement after connecting. The statement can be used regardless of connection method: The mysql client, PHP scripts, and so forth.

In some cases, it may be possible to configure the connection to use the desired character set some other way. For example, for connections made using mysql, you can specify the --default-character-set=utf8 command-line option to achieve the same effect as SET NAMES 'utf8'.

If you change the default character set or collation for a database, stored routines that use the database defaults must be dropped and recreated so that they use the new defaults. (In a stored routine, variables with character data types use the database defaults if the character set or collation are not specified explicitly.

Specify character settings at server startup. To select a character set and collation at server startup, use the --character-set-server and --collation-server options. For example, to specify the options in an option file, include these lines:

[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
These settings apply server-wide and apply as the defaults for databases created by any application, and for tables created in those databases.

I hope these information would be useful to you.

关于php - 如何将特殊字符以UTF-8格式存储在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35149841/

相关文章:

php - 在 Laravel 中为现有数据库中的存储过程、函数和事件创建迁移

java - 在 Hibernate 3.2 中使用连接池 (c3p0-0.9.1.2) 时出现异常且应用程序无法连接 MySqL 数据库?

MySQL数据删除无删除级联

node.js - 使用 NodeJS 终端或 iisnode 是个好主意吗

php - 什么是 'module_number' 作为 zend_register_list_destructors_ex 的参数

php - 如何在 PHP while 循环中隐藏没有 url 的类别

php - 比较运算符 Sql 查询与 php

php - 获取数组时没有结果出现 - PHP/MYSQL

asp.net - 将文件上传到不同的服务器

c# - 为什么 IDisposable 立即调用 Dispose()?