CodeIgniter使用字符串中的引号将数据插入数据库

时间:2015-11-01 16:35:11

标签: php mysql codeigniter activerecord codeigniter-3

数据看起来像

$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;

2 个答案:

答案 0 :(得分: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
);

答案 1 :(得分:1)

这是客户端角色的问题。什么时候,基于我以前在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 Workbench

我从来没有遇到任何问题,即使从一开始。所以它有一个不同的client characterset

你的世界

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