0

data is look like

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

I use

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

When i run

$this->db->last_query()

I get:

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

and Data saved as

How to say ?Something? in

And the

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

is run as

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

Is inserted as

"My Name is?"

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

is inserted as

What?s your name?

And Question table is like

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;
8
  • 1
    is it just some funky double quotes you cut and paste off the web? Commented Nov 1, 2015 at 16:43
  • I get data from excel file uploaded by the client Commented Nov 1, 2015 at 18:30
  • perhaps edit the question and show more info of the section of code or the flow that could intercept this and tweak it. Perhaps lose a tag and add Excel Commented Nov 1, 2015 at 19:20
  • 1
    can you show a schema of table questions and the actual raw string that is performed without ellipses I am confused how to help. Perhaps the issue is just getting the correct unicode value for the fancy quotes, and using the str_replace in Abdulla's answer Commented Nov 2, 2015 at 1:40
  • 1
    ok I see what you see. Just a second Commented Nov 2, 2015 at 2:10

2 Answers 2

1

Use htmlentities()

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

Or

Use str_replace()

$value = 'How to say “something” in';
$data = array
(
    'question' => str_replace('"', '', $value);,
    'answer_one' => '1. Some Answer'
     
     'correct_answer' => 1
);
Sign up to request clarification or add additional context in comments.

1 Comment

didn't worked since " “ " and " ” " are not quotes :(
1

It is an issue with the client characterset. When, based on what I had before in mysql command line client (when in client), and I run status;, I get, among other things:

mysql> status;

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

Then:

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 |
+-------------+----------------------------+----------------+----------------+---------------------+

Even creating another database like:

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;

or

ALTER TABLE questions CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin;

Did not make a difference.

It wasn't until I ran

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)

was it good. Note when I run status; now I get

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

Refer to the mysql manual page entitled Connection Character Sets and Collations

Connection Lifetime

That worked fine, til I started the client up fresh again. So there are INI file or CONF file settings

Mysql Workbench

I never had an issue over there at all, even from the beginning. So it has a different client characterset

Your World

Whatever that connection is, it needs to get the client characterset over to UTF8

4 Comments

When i chage to UTF-8 data saved only "How to say", "My Name is", "What"
what technically is the client app? Is it mysql command line utility, a PHP connection? Show the code in detail, maybe in a pastie.org page if you need to. Then describe what other angle (what code, what utility) you went in to see the data wrong. There are too many permutations to make assumptions here
it is a codeigniter application. I'm using active record and query builder of it.
$this->db->insert('questions',$data);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.