【问题标题】:CodeIgniter inserting data to database with quotes in stringCodeIgniter 使用字符串中的引号将数据插入数据库
【发布时间】:2015-11-01 16:35:10
【问题描述】:

数据看起来像

$data = array
(
'question' => 'How to say “something” in'
'answer_one' => '1. Some Answer'
...
'correct_answer' => 1
);

我用

$this->db->insert('questions',$data);

当我跑步时

$this->db->last_query()

我明白了:

INSERT INTO `questions` (`question`, `answer_one`, ... , `correct_answer`) VALUES ('How to say “Something”  in', '1. Some Answer', ... ,'1')

和数据保存为

怎么说?有什么?在

还有

$data = array
(
'question' => 'My Name is…'
'answer_one' => '1. Some Answer'
...
'correct_answer' => 1
);

运行方式

INSERT INTO `questions` (`question`, `answer_one`, ... , `correct_answer`) VALUES ('My Name is…', '1. Name', ... , '1')

插入为

“我的名字是?”

$data = array
(
'question' => 'What's your name?'
'answer_one' => '1. Some Answer'
...
'correct_answer' => 1
);

插入为

你叫什么名字?

问题表是这样的

CREATE TABLE `questions` (
 `question_id` int(11) NOT NULL AUTO_INCREMENT,
 `question` varchar(200),
 `answer_one` varchar(80),
 ...
 `correct_answer` int(2),
 `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
 PRIMARY KEY (`question_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

【问题讨论】:

  • 只是你从网上剪切和粘贴的一些时髦的双引号吗?
  • 我从客户端上传的excel文件中获取数据
  • 也许是edit 的问题,并显示代码部分的更多信息或可以拦截和调整它的流程。可能会丢失标签并添加Excel
  • 您能否显示表questions 的架构以及在没有省略号的情况下执行的实际原始字符串我很困惑如何提供帮助。也许问题只是为花哨的引号获取正确的 unicode 值,并在 Abdulla 的答案中使用 str_replace
  • 好的,我明白了。只需一秒钟

标签: php mysql codeigniter activerecord codeigniter-3


【解决方案1】:

使用htmlentities()

$data = array
(
    'question' => htmlentities('How to say “something” in'),
    'answer_one' => '1. Some Answer'
    ...
    'correct_answer' => 1
);

或者

使用str_replace()

$value = 'How to say “something” in';
$data = array
(
    'question' => str_replace('"', '', $value);,
    'answer_one' => '1. Some Answer'

     'correct_answer' => 1
);

【讨论】:

  • 没有用,因为“”和“””不是引号:(
【解决方案2】:

这是客户端字符集的问题。什么时候,根据我之前在 mysql 命令行客户端中的内容(在客户端中时),我运行 status;,除其他外,我得到:

mysql> 状态;

mysql  Ver 14.14 Distrib 5.6.24, for Win64 (x86_64)

Connection id:          88
Server version:         5.6.24-log MySQL Community Server (GPL)
Protocol version:       10
Connection:             localhost via TCP/IP
Server characterset:    utf8
Db     characterset:    utf8
Client characterset:    cp850
Conn.  characterset:    cp850

然后:

mysql> use so_gibberish;
Database changed
mysql> select * from questions;
+-------------+----------------------------+----------------+----------------+---------------------+
| question_id | question                   | answer_one     | correct_answer | created_at          |
+-------------+----------------------------+----------------+----------------+---------------------+
|           1 | How to say ?Something?  in | 1. Some Answer |              1 | 2015-11-01 21:06:23 |
+-------------+----------------------------+----------------+----------------+---------------------+

甚至创建另一个数据库,例如:

CREATE DATABASE newdb CHARACTER SET utf8 COLLATE utf8_bin;
use newdb;

CREATE TABLE `questions` (
 `question_id` int(11) NOT NULL AUTO_INCREMENT,
 `question` varchar(200) CHARACTER SET utf8 COLLATE utf8_bin, -- utf8_unicode_ci,
 `answer_one` varchar(80),
 `correct_answer` int(2),
 `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
 PRIMARY KEY (`question_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

ALTER TABLE questions CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin;

没有影响。

直到我跑了

mysql> charset utf8;
Charset changed
mysql> select * from questions;
+-------------+--------------------------------+----------------+----------------+---------------------+
| question_id | question                       | answer_one     | correct_answer | created_at          |
+-------------+--------------------------------+----------------+----------------+---------------------+
|           1 | How to say "Something"  in     | 1. Some Answer |              1 | 2015-11-01 21:06:23 |
+-------------+--------------------------------+----------------+----------------+---------------------+
1 row in set (0.00 sec)

还好吗。请注意,当我现在运行 status; 时,我得到了

Server characterset:    utf8
Db     characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8

参考题为Connection Character Sets and Collations的mysql手册页

连接寿命

效果很好,直到我重新启动客户端。所以有INI文件或者CONF文件设置

Mysql 工作台

我从来没有在那边遇到过问题,即使是从一开始。所以它有一个不同的client characterset

你的世界

无论该连接是什么,它都需要将 client characterset 转换为 UTF8

【讨论】:

  • 当我转换为 UTF-8 数据时,只保存“怎么说”、“我的名字”、“什么”
  • 从技术上讲,客户端应用程序是什么?它是 mysql 命令行实用程序,PHP 连接吗?详细显示代码,如果需要,可能在pastie.org 页面中。然后描述你在其他哪个角度(什么代码,什么实用程序)看到数据错误。这里有太多的排列来做假设
  • 它是一个codeigniter应用程序。我正在使用它的活动记录和查询生成器。
  • $this->db->insert('questions',$data);
猜你喜欢
  • 1970-01-01
  • 2013-09-29
  • 2013-04-04
  • 2016-09-18
  • 2013-01-28
  • 2018-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多